思路分析:
利用一个指针ptr进行遍历,每次循环遍历过整个链表后,取出链表中节点的最小值连接到一个新的链表res后,同时将取最小值的节点值置为INT_MAX。
代码实现:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
public:
ListNode* sortList(ListNode* head)
{
ListNode *res=new ListNode,*h=res,*ptr,*i=head,*k;
while(i!=nullptr)
{
int min=INT_MAX;
ptr=head;
while(ptr!=nullptr)
{
if(min>ptr->val)
{
min=ptr->val;
k=ptr;
}
ptr=ptr->next;
}
k->val=INT_MAX;
res->next=new ListNode;
res=res->next;
res->val=min;
i=i->next;
}
return h->next;
}
};
问题分析:
此思路想法没有问题,但是时间复杂度太高,可以解决较短的链表的排序问题,但当leetcode给出一串长达50000个节点的链表时,终于超出了时间限制。
换种思路:
利用快慢指针,将链表分成两部分分别进行排序。
利用递归让链表不断切分,分链表中只剩下一个元素时,不必再进行切分。
利用合并两个有序链表的思路,对切分后的链表进行重新合并。
最后返回合并后的链表。
代码实现:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution
{
public:
ListNode* sortList(ListNode* head)
{
return sortList(head, nullptr);
}
ListNode* sortList(ListNode* head, ListNode* tail)
{
if (head == nullptr)
return head;
if (head->next == tail)
{
head->next = nullptr;
return head;
}
ListNode* slow = head, *fast = head;
while (fast != tail)
{
slow = slow->next;
fast = fast->next;
if (fast != tail)
fast = fast->next;
}
ListNode* mid = slow;
return merge(sortList(head, mid), sortList(mid, tail));
}
ListNode* merge(ListNode* head1, ListNode* head2)
{
ListNode* dummyHead = new ListNode(0);
ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
while (temp1 != nullptr && temp2 != nullptr)
{
if (temp1->val <= temp2->val)
{
temp->next = temp1;
temp1 = temp1->next;
}
else
{
temp->next = temp2;
temp2 = temp2->next;
}
temp = temp->next;
}
if (temp1 != nullptr)
temp->next = temp1;
else if (temp2 != nullptr)
temp->next = temp2;
return dummyHead->next;
}
};
运行结果: