-
Notifications
You must be signed in to change notification settings - Fork 0
/
148.排序链表.cpp
76 lines (66 loc) · 1.87 KB
/
148.排序链表.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* @lc app=leetcode.cn id=148 lang=cpp
*
* [148] 排序链表
*/
// @lc code=start
/**
* 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) {
if (head == NULL || head->next == NULL)
return head;
return mergeSort(head);
}
ListNode* mergeSort(ListNode *head) {
if (head == NULL || head->next == NULL) return head;
ListNode *fast = head->next;
ListNode *slow = head;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
slow = slow->next;
}
// Slow 是前半部分L1的最后一个结点, 它的next 一定要设置为nullptr
ListNode *L2 = slow->next; // L2从slow的next 开始
slow->next = NULL;
head = mergeSort(head);
L2 = mergeSort(L2);
return mergeTwoList(head, L2);
}
ListNode* mergeTwoList(ListNode *L1, ListNode *L2) {
ListNode *dummyHead = new ListNode;
ListNode *tail = dummyHead;
while (L1 != NULL && L2 != NULL) {
if (L1->val <= L2->val) {
tail->next = L1;
L1 = L1->next;
tail = tail->next;
} else {
tail->next = L2;
L2 = L2->next;
tail = tail->next;
}
}
while (L1 != NULL) {
tail->next = L1;
L1 = L1->next;
tail = tail->next;
}
while (L2 != NULL) {
tail->next = L2;
L2 = L2->next;
tail = tail->next;
}
return dummyHead->next;
}
};
// @lc code=end