题目:
Sort a linked list in O(n log n) time using constant space complexity.
C++解答:
思路:利用归并排序,时间复杂度满足要求。
#include<iostream>
using namespace std;
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* merge(ListNode *Left, ListNode* Right)
{
if(Left==NULL)
return Right;
if(Right==NULL)
return Left;
ListNode* Merge;
ListNode *p3, *r;
Merge = p3 = new ListNode(NULL);
while(Left!=NULL && Right!=NULL)
{
r = new ListNode(NULL);
if(Left->val>=Right->val)
{
p3->val = Right->val;
Right = Right->next;
}
else
{
p3->val = Left->val;
Left = Left->next;
}
//这个地方一定要判断,之前没有判断就直接赋值,得到的结果中会夹杂着0,因为将r加入后,跳出循环的话,它又是下一个节点赋值了
if(Left!=NULL && Right!=NULL)
{
p3->next = r;
p3 = r;
}
}
if(Left==NULL && Right!=NULL)
{
p3->next = Right;
}
if(Right==NULL && Left!=NULL)
{
p3->next = Left;
}
p3 = NULL;
return Merge;
}
class Solution {
public:
ListNode *sortList(ListNode *head) {
if(head==NULL || head->next==NULL)
return head;
//用两个指针进行找链表中间的数
ListNode *p1, *p2;
p1 = p2 = head;
//前半段和后半段的临时链表
ListNode* left = p1;
while(p2!=NULL && p2->next!=NULL && p2->next->next!=NULL)
{
p1 = p1->next;
p2 = p2->next->next;
}
ListNode* right = p1->next;
p1->next = NULL; //左边链表建立
//分别对前后段进行分裂,最后合并
ListNode* Left = sortList(left);
ListNode* Right = sortList(right);
return merge(Left, Right);
}
};
ListNode* creatList(int A[], int m)
{
ListNode *head, *r, *s;
head = r = new ListNode(0);
for(int i=0; i<m; ++i)
{
s = new ListNode(0);
r->val = A[i];
if(i<m-1)
{
r->next = s;
r = s;
}
else
r->next=NULL;
}
s = NULL;
return head;
}
int main()
{
int A[] = {2, 5, 16, 7};
int B[] = {35, 3, 12, 6, 7};
int m = sizeof(A)/sizeof(int);
int n = sizeof(B)/sizeof(int);
ListNode *l1 = creatList(A, m);
ListNode *l2 = creatList(B, n);
Solution s;
ListNode* result = s.sortList(l1);
for(ListNode* i=result; i!=NULL; i=i->next)
{
cout << i->val << endl;
}
system("pause");
return 0;
}