C++将链表划分为左边小,中间等,右边大于pivotKey的形式
2017-08-09 09:46
387 查看
#include <iostream>
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {};
};
Node *ListPartition(Node * head, int pivot);
void swap(int arr[],int low, int high);
void partition(int arr[], int index, int length, int pivot);
int main()
{
Node n1(3);
Node n2(5);
Node n3(7);
Node n4(2);
Node n5(4);
Node n6(1);
Node n7(0);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = nullptr;
Node * cur = ListPartition(&n1, 3);
while (cur != nullptr)
{
std::cout << cur->value << std::endl;
cur = cur->next;
}
system("pause");
return 0;
}
Node *ListPartition(Node * head, int pivot)
{
if (head == nullptr || head->next == nullptr)
{
return head;
}
int length = 0;
Node *cur = head;
while (cur != nullptr)
{
++length;
cur = cur->next;
}
cur = head;
int *pArr = (int *)malloc(length);
for (int i = 0; i < length; ++i)
{
pArr[i] = cur->value;
cur = cur->next;
}
cur = head;
partition(pArr, 0, length, pivot);
for (int i = 0; i < length; ++i)
{
cur->value = pArr[i];
cur = cur->next;
}
//free(pArr);
return head;
}
void swap(int arr[], int low, int high)
{
int tmp = arr[low];
arr[low] = arr[high];
arr[high] = tmp;
}
void partition(int arr[], int index, int length, int pivot)
{
int Lp = -1;
int Hp = length - 1;
while (index < Hp)
{
if (index < Hp && arr[index] < pivot)
{
swap(arr, ++Lp, index++);
}
if (index < Hp && arr[index] == pivot)
++index;
else if (index < Hp && arr[index] > pivot)
swap(arr, Hp--, index);
}
}
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {};
};
Node *ListPartition(Node * head, int pivot);
void swap(int arr[],int low, int high);
void partition(int arr[], int index, int length, int pivot);
int main()
{
Node n1(3);
Node n2(5);
Node n3(7);
Node n4(2);
Node n5(4);
Node n6(1);
Node n7(0);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = nullptr;
Node * cur = ListPartition(&n1, 3);
while (cur != nullptr)
{
std::cout << cur->value << std::endl;
cur = cur->next;
}
system("pause");
return 0;
}
Node *ListPartition(Node * head, int pivot)
{
if (head == nullptr || head->next == nullptr)
{
return head;
}
int length = 0;
Node *cur = head;
while (cur != nullptr)
{
++length;
cur = cur->next;
}
cur = head;
int *pArr = (int *)malloc(length);
for (int i = 0; i < length; ++i)
{
pArr[i] = cur->value;
cur = cur->next;
}
cur = head;
partition(pArr, 0, length, pivot);
for (int i = 0; i < length; ++i)
{
cur->value = pArr[i];
cur = cur->next;
}
//free(pArr);
return head;
}
void swap(int arr[], int low, int high)
{
int tmp = arr[low];
arr[low] = arr[high];
arr[high] = tmp;
}
void partition(int arr[], int index, int length, int pivot)
{
int Lp = -1;
int Hp = length - 1;
while (index < Hp)
{
if (index < Hp && arr[index] < pivot)
{
swap(arr, ++Lp, index++);
}
if (index < Hp && arr[index] == pivot)
++index;
else if (index < Hp && arr[index] > pivot)
swap(arr, Hp--, index);
}
}
相关文章推荐
- C++链表划分左边小中间等右边大 时间复杂度O(n) 空间复杂度O(1)
- 将单向链表按某值划分成左边小、 中间相等、 右边大的形式~迎娶是挺
- 将单向链表按某值划分成左边小、中间相等、右边大的形式
- [链表] - 将单向链表按某值划分成左边小、中间相等、右边大的形式
- 链表问题---将单向链表按某值划分成左边小,中间相等,右边大的形式
- 将单向链表按某值划分成左边小、中间相等、右边大的形式
- Java——将单向链表按某值划分成左边小、中间相等、右边大的形式(进阶问题)
- 将单向链表按某个值划分成左边小、中间相等、右边大的形式 Python 版
- Java——将单向链表按某值划分成左边小、中间相等、右边大的形式(基础问题)
- [JS]链表划分给定中间值,小的在左边,大的在右边并保持原排序
- 把单链表按某值划分成左边小,中间相等,右边大的形式,时间O(n),空间O(1)
- 将单向链表按某值划分成左边小,中间相等,右边大
- [JS]链表划分给定中间值,小的在左边,大的在右边并保持原排序
- 据说是淘宝面试题“给定一个数组将大于0的放在最右边,等于0的放在中间,大于0的放在最左边”
- 划分一个整形数组,把负数放在左边,零放在中间,正数放在右边
- 一个int数组, 比如 array[],里面数据无任何限制,要求求出 所有这样的数array[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现
- c++中对单链表操作---合并两个链表&&单链表逆序&&删除链表中的中间结点
- 左神的书——《程序员代码面试指南》之删除链表的中间节点或a/b节点 c++实现
- C#字符串操作 取文本左边 取文本右边 取文本中间 取文本中间到List集合 指定文本倒序
- int数组,找小于右边所有数,大于左边所有数的数