LinkNode* getPartition(LinkNode* head){
LinkNode* tail = head;
while(tail->next!=nullptr){
tail = tail->next;
}
LinkNode* go = head->next;
LinkNode* small = nullptr;
LinkNode* equal = nullptr;
LinkNode* big = nullptr;
//第一次遍历,我们要找到第一个 与最后一个小的、和最后一个相等的,比最后一个大的
//注意空值得边界
while(go!=nullptr){
if(small == nullptr || equal==nullptr || big == nullptr){
if(small==nullptr && go->value < tail->value){
small = go;
}else if(equal==nullptr && go->value == tail->value){
equal = go;
}else if(big ==nullptr && go->value > tail->value){
big = go;
}
}
go = go->next;
}
LinkNode* small_go = small;
LinkNode* equal_go = equal;
LinkNode* big_go = big;
go = head->next;
//遍历链表,在链表中,除了上面找到的三个节点(可能没有三个),其他比目标小的串在small链表中,
//与目标相等的串在equal中。比目标大的串在big中(尾插法)
while(go!=nullptr){
if(go == small || go == equal || go == big){
go = go->next;
continue;
}
if(go->value < tail->value){
small_go->next = go;
small_go = small_go->next;
}else if(go->value == tail->value){
equal_go->next = go;
equal_go = equal_go->next;
}else{
big_go->next = go;
big_go = big_go->next;
}
go = go->next;
}
// 连接三串链表,注意边界
go = head;
if(small!=nullptr){
go->next = small;
go = small_go;
}
if(equal!=nullptr){
go->next = equal;
go = equal_go;
}
if(big!= nullptr){
go->next = big;
go = big_go;
}
go->next = nullptr;
return head;
}
以下是包含测试的代码:
#include<iostream>
#include<stack>
using namespace std;
struct LinkNode{
int value;
LinkNode* next;
};
LinkNode* getLink(int arr[], int length);
void show(LinkNode* head);
LinkNode* getPartition(LinkNode* head);
int main(){
// int arr1[] = {1,3,2,4,7,8,4,3};
// int arr1[] = {5,7,8,4};
// int arr1[] = {7,8,1,2,9};
int arr1[] = {1,1,1,1,1};
int length = sizeof(arr1)/sizeof(arr1[0]);
LinkNode* head1 = getLink(arr1,length);
LinkNode* partition = getPartition(head1);
show(partition);
return 0;
}
LinkNode* getLink(int arr[], int length){
LinkNode* head = new LinkNode();
LinkNode* go = head;
int i =0;
while(i<length){
LinkNode* node = new LinkNode();
node->value = arr[i];
node->next = nullptr;
go->next = node;
go = go->next;
i++;
}
return head;
}
void show(LinkNode* head){
LinkNode* go = head->next;
while(go!=nullptr){
cout << go->value << endl;
go = go->next;
}
}
LinkNode* getPartition(LinkNode* head){
LinkNode* tail = head;
while(tail->next!=nullptr){
tail = tail->next;
}
LinkNode* go = head->next;
LinkNode* small = nullptr;
LinkNode* equal = nullptr;
LinkNode* big = nullptr;
//第一次遍历,我们要找到第一个 与最后一个小的、和最后一个相等的,比最后一个大的
//注意空值得边界
while(go!=nullptr){
if(small == nullptr || equal==nullptr || big == nullptr){
if(small==nullptr && go->value < tail->value){
small = go;
}else if(equal==nullptr && go->value == tail->value){
equal = go;
}else if(big ==nullptr && go->value > tail->value){
big = go;
}
}
go = go->next;
}
LinkNode* small_go = small;
LinkNode* equal_go = equal;
LinkNode* big_go = big;
go = head->next;
//遍历链表,在链表中,除了上面找到的三个节点(可能没有三个),其他比目标小的串在small链表中,
//与目标相等的串在equal中。比目标大的串在big中(尾插法)
while(go!=nullptr){
if(go == small || go == equal || go == big){
go = go->next;
continue;
}
if(go->value < tail->value){
small_go->next = go;
small_go = small_go->next;
}else if(go->value == tail->value){
equal_go->next = go;
equal_go = equal_go->next;
}else{
big_go->next = go;
big_go = big_go->next;
}
go = go->next;
}
// 连接三串链表,注意边界
go = head;
if(small!=nullptr){
go->next = small;
go = small_go;
}
if(equal!=nullptr){
go->next = equal;
go = equal_go;
}
if(big!= nullptr){
go->next = big;
go = big_go;
}
go->next = nullptr;
return head;
}