C++链表划分左边小中间等右边大 时间复杂度O(n) 空间复杂度O(1)
2017-08-09 11:52
459 查看
#include <iostream>
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {};
};
Node * List_Partition_Pro(Node *head, int pivot);
void Print(Node *head);
int main()
{
Node n1(3);
Node n2(5);
Node n3(7);
Node n4(4);
Node n5(6);
Node n6(2);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = nullptr;
std::cout << "Before Partition: " << std::endl;
Print(&n1);
std::cout << "After Partition: " << std::endl;
Print(List_Partition_Pro(&n1, 4));
system("pause");
return 0;
}
Node * List_Partition_Pro(Node *head, int pivot)
{
Node * SH = nullptr, * ST = nullptr;
Node * MH = nullptr, * MT = nullptr;
Node * BH = nullptr, * BT = nullptr;
Node * next = nullptr;
while (head != nullptr){
next = head->next;
head->next = nullptr;
if (head->value < pivot) {
if (SH == nullptr) {
SH = head;
ST = head;
}else{
ST->next = head;
ST = head;
}
}
if (head->value == pivot) {
if (MH == nullptr) {
MH = head;
MT = head;
} else {
MT->next = head;
MT = head;
}
}
if (head->value > pivot){
if (BH == nullptr){
BH = head;
BT = head;
}else{
BT->next = head;
BT = head;
}
}
head = next;
}
if (ST != nullptr){
ST->next = MH;
MT = MT == nullptr ? ST : MT;
}
if (MT != nullptr){
MT->next = BH;
}
return head = ST == nullptr ? MT == nullptr ? BH : MH : SH;
}
void Print(Node *head)
{
while (head != nullptr) {
std::cout << head->value << std::endl;
head = head->next;
}
}
struct Node {
int value;
Node *next;
Node(int data) : value(data), next(nullptr) {};
};
Node * List_Partition_Pro(Node *head, int pivot);
void Print(Node *head);
int main()
{
Node n1(3);
Node n2(5);
Node n3(7);
Node n4(4);
Node n5(6);
Node n6(2);
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = nullptr;
std::cout << "Before Partition: " << std::endl;
Print(&n1);
std::cout << "After Partition: " << std::endl;
Print(List_Partition_Pro(&n1, 4));
system("pause");
return 0;
}
Node * List_Partition_Pro(Node *head, int pivot)
{
Node * SH = nullptr, * ST = nullptr;
Node * MH = nullptr, * MT = nullptr;
Node * BH = nullptr, * BT = nullptr;
Node * next = nullptr;
while (head != nullptr){
next = head->next;
head->next = nullptr;
if (head->value < pivot) {
if (SH == nullptr) {
SH = head;
ST = head;
}else{
ST->next = head;
ST = head;
}
}
if (head->value == pivot) {
if (MH == nullptr) {
MH = head;
MT = head;
} else {
MT->next = head;
MT = head;
}
}
if (head->value > pivot){
if (BH == nullptr){
BH = head;
BT = head;
}else{
BT->next = head;
BT = head;
}
}
head = next;
}
if (ST != nullptr){
ST->next = MH;
MT = MT == nullptr ? ST : MT;
}
if (MT != nullptr){
MT->next = BH;
}
return head = ST == nullptr ? MT == nullptr ? BH : MH : SH;
}
void Print(Node *head)
{
while (head != nullptr) {
std::cout << head->value << std::endl;
head = head->next;
}
}
相关文章推荐
- C++将链表划分为左边小,中间等,右边大于pivotKey的形式
- [JS]链表划分给定中间值,小的在左边,大的在右边并保持原排序
- 把单链表按某值划分成左边小,中间相等,右边大的形式,时间O(n),空间O(1)
- 将单向链表按某值划分成左边小、 中间相等、 右边大的形式~迎娶是挺
- 链表问题---将单向链表按某值划分成左边小,中间相等,右边大的形式
- Java——将单向链表按某值划分成左边小、中间相等、右边大的形式(基础问题)
- 将单向链表按某值划分成左边小、中间相等、右边大的形式
- 设计算法将线性表中的元素分为两部分,要求左边都是奇数,右边都是偶数,时间复杂度O(n),空间复杂度O(1)
- [JS]链表划分给定中间值,小的在左边,大的在右边并保持原排序
- [链表] - 将单向链表按某值划分成左边小、中间相等、右边大的形式
- Java——将单向链表按某值划分成左边小、中间相等、右边大的形式(进阶问题)
- 将单向链表按某值划分成左边小,中间相等,右边大
- 将单向链表按某个值划分成左边小、中间相等、右边大的形式 Python 版
- 将单向链表按某值划分成左边小、中间相等、右边大的形式
- 给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。 要求:空间复杂度O(1),时间复杂度为O(n)。
- 反转链表,时间复杂度O(n),空间复杂度O(1)
- 已知一个整数数组A[n],写出算法实现将奇数元素放在数组的左边,将偶数放在数组的右边。要求时间复杂度为O(n)。
- 如何重新排列数组使得数组左边为奇数,右边为偶数,并使得空间复杂度为O(1),时间复杂度为O(n)
- 百度笔试:给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。 要求:空间复杂度O(1),时间复杂度为O(n)
- 设一个没有头结点指针的单链表。一个指针指向此单链表中间的一个结点(不是第一个,也不是最后一个结点),将该结点从单链表中删除,要求时间复杂度O(1)。