Skip to content

Commit bac2206

Browse files
committed
Synced from java to cpp
1 parent 57f2ba5 commit bac2206

4 files changed

Lines changed: 199 additions & 28 deletions

File tree

‎cpp/code/sort-list.cpp‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,17 @@ class Solution {
55
ListNode *sortList(ListNode *head) {
66
if (head == NULL || head->next == NULL)return head;
77

8-
// 快慢指针找到中间节点
9-
ListNode *fast = head, *slow = head;
10-
while (fast->next != NULL && fast->next->next != NULL) {
11-
fast = fast->next->next;
12-
slow = slow->next;
13-
}
14-
// 断开
15-
fast = slow;
16-
slow = slow->next;
17-
fast->next = NULL;
8+
ListNode *middle = findMiddle(head);
9+
ListNode *head2 = middle->next;
10+
middle->next = nullptr; // 断开
1811

1912
ListNode *l1 = sortList(head); // 前半段排序
20-
ListNode *l2 = sortList(slow); // 后半段排序
13+
ListNode *l2 = sortList(head2); // 后半段排序
2114
return mergeTwoLists(l1, l2);
2215
}
23-
16+
private:
2417
// Merge Two Sorted Lists
25-
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
18+
static ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
2619
ListNode dummy(-1);
2720
for (ListNode* p = &dummy; l1 != nullptr || l2 != nullptr; p = p->next) {
2821
int val1 = l1 == nullptr ? INT_MAX : l1->val;
@@ -37,4 +30,17 @@ class Solution {
3730
}
3831
return dummy.next;
3932
}
33+
// 快慢指针找到中间节点
34+
static ListNode* findMiddle(ListNode* head) {
35+
if (head == nullptr) return nullptr;
36+
37+
ListNode *slow = head;
38+
ListNode *fast = head->next;
39+
40+
while (fast != nullptr && fast->next != nullptr) {
41+
slow = slow->next;
42+
fast = fast->next->next;
43+
}
44+
return slow;
45+
}
4046
};

‎cpp/code/sort-list.java‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,17 @@ public class Solution {
44
public ListNode sortList(ListNode head) {
55
if (head == null || head.next == null)return head;
66

7-
// 快慢指针找到中间节点
8-
ListNode fast = head, slow = head;
9-
while (fast.next != null && fast.next.next != null) {
10-
fast = fast.next.next;
11-
slow = slow.next;
12-
}
13-
// 断开
14-
fast = slow;
15-
slow = slow.next;
16-
fast.next = null;
7+
final ListNode middle = findMiddle(head);
8+
final ListNode head2 = middle.next;
9+
middle.next = null; // 断开
1710

18-
ListNode l1 = sortList(head); // 前半段排序
19-
ListNode l2 = sortList(slow); // 后半段排序
11+
final ListNode l1 = sortList(head); // 前半段排序
12+
final ListNode l2 = sortList(head2); // 后半段排序
2013
return mergeTwoLists(l1, l2);
2114
}
2215

2316
// Merge Two Sorted Lists
24-
ListNode mergeTwoLists(ListNode l1, ListNode l2) {
17+
private static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
2518
ListNode dummy = new ListNode(-1);
2619
for (ListNode p = dummy; l1 != null || l2 != null; p = p.next) {
2720
int val1 = l1 == null ? Integer.MAX_VALUE : l1.val;
@@ -36,4 +29,17 @@ ListNode mergeTwoLists(ListNode l1, ListNode l2) {
3629
}
3730
return dummy.next;
3831
}
32+
// 快慢指针找到中间节点
33+
private static ListNode findMiddle(ListNode head) {
34+
if (head == null) return null;
35+
36+
ListNode slow = head;
37+
ListNode fast = head.next;
38+
39+
while (fast != null && fast.next != null) {
40+
slow = slow.next;
41+
fast = fast.next.next;
42+
}
43+
return slow;
44+
}
3945
}

‎cpp/sorting/insertion-sort-list.md‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,67 @@ Sort a linked list using insertion sort.
1313

1414
### 代码
1515

16-
{% codesnippet "./code/insertion-sort-list."+book.suffix, language=book.suffix %}{% endcodesnippet %}
16+
17+
{% if book.java %}
18+
```java
19+
// Insertion Sort List
20+
// 时间复杂度O(n^2),空间复杂度O(1)
21+
public class Solution {
22+
public ListNode insertionSortList(ListNode head) {
23+
ListNode dummy = new ListNode(Integer.MIN_VALUE);
24+
//dummy.next = head;
25+
26+
for (ListNode cur = head; cur != null;) {
27+
ListNode pos = findInsertPos(dummy, cur.val);
28+
ListNode tmp = cur.next;
29+
cur.next = pos.next;
30+
pos.next = cur;
31+
cur = tmp;
32+
}
33+
return dummy.next;
34+
}
35+
36+
ListNode findInsertPos(ListNode head, int x) {
37+
ListNode pre = null;
38+
for (ListNode cur = head; cur != null && cur.val <= x;
39+
pre = cur, cur = cur.next)
40+
;
41+
return pre;
42+
}
43+
}
44+
```
45+
{% endif %}
46+
47+
{% if book.cpp %}
48+
```cpp
49+
// Insertion Sort List
50+
// 时间复杂度O(n^2),空间复杂度O(1)
51+
class Solution {
52+
public:
53+
ListNode *insertionSortList(ListNode *head) {
54+
ListNode dummy(INT_MIN);
55+
//dummy.next = head;
56+
57+
for (ListNode *cur = head; cur != nullptr;) {
58+
auto pos = findInsertPos(&dummy, cur->val);
59+
ListNode *tmp = cur->next;
60+
cur->next = pos->next;
61+
pos->next = cur;
62+
cur = tmp;
63+
}
64+
return dummy.next;
65+
}
66+
67+
ListNode* findInsertPos(ListNode *head, int x) {
68+
ListNode *pre = nullptr;
69+
for (ListNode *cur = head; cur != nullptr && cur->val <= x;
70+
pre = cur, cur = cur->next)
71+
;
72+
return pre;
73+
}
74+
};
75+
```
76+
{% endif %}
1777
1878
1979
### 相关题目

‎cpp/sorting/sort-list.md‎

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,106 @@ Sort a linked list in `O(n log n)` time using constant space complexity.
1313

1414
### 代码
1515

16-
{% codesnippet "./code/sort-list."+book.suffix, language=book.suffix %}{% endcodesnippet %}
16+
{% if book.java %}
17+
```java
18+
// Sort List
19+
// 归并排序,时间复杂度O(nlogn),空间复杂度O(1)
20+
public class Solution {
21+
public ListNode sortList(ListNode head) {
22+
if (head == null || head.next == null)return head;
23+
24+
final ListNode middle = findMiddle(head);
25+
final ListNode head2 = middle.next;
26+
middle.next = null; // 断开
27+
28+
final ListNode l1 = sortList(head); // 前半段排序
29+
final ListNode l2 = sortList(head2); // 后半段排序
30+
return mergeTwoLists(l1, l2);
31+
}
32+
33+
// Merge Two Sorted Lists
34+
private static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
35+
ListNode dummy = new ListNode(-1);
36+
for (ListNode p = dummy; l1 != null || l2 != null; p = p.next) {
37+
int val1 = l1 == null ? Integer.MAX_VALUE : l1.val;
38+
int val2 = l2 == null ? Integer.MAX_VALUE : l2.val;
39+
if (val1 <= val2) {
40+
p.next = l1;
41+
l1 = l1.next;
42+
} else {
43+
p.next = l2;
44+
l2 = l2.next;
45+
}
46+
}
47+
return dummy.next;
48+
}
49+
// 快慢指针找到中间节点
50+
private static ListNode findMiddle(ListNode head) {
51+
if (head == null) return null;
52+
53+
ListNode slow = head;
54+
ListNode fast = head.next;
55+
56+
while (fast != null && fast.next != null) {
57+
slow = slow.next;
58+
fast = fast.next.next;
59+
}
60+
return slow;
61+
}
62+
}
63+
```
64+
{% endif %}
65+
66+
{% if book.cpp %}
67+
```cpp
68+
// Sort List
69+
// 归并排序,时间复杂度O(nlogn),空间复杂度O(1)
70+
class Solution {
71+
public:
72+
ListNode *sortList(ListNode *head) {
73+
if (head == NULL || head->next == NULL)return head;
74+
75+
ListNode *middle = findMiddle(head);
76+
ListNode *head2 = middle->next;
77+
middle->next = nullptr; // 断开
78+
79+
ListNode *l1 = sortList(head); // 前半段排序
80+
ListNode *l2 = sortList(head2); // 后半段排序
81+
return mergeTwoLists(l1, l2);
82+
}
83+
private:
84+
// Merge Two Sorted Lists
85+
static ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
86+
ListNode dummy(-1);
87+
for (ListNode* p = &dummy; l1 != nullptr || l2 != nullptr; p = p->next) {
88+
int val1 = l1 == nullptr ? INT_MAX : l1->val;
89+
int val2 = l2 == nullptr ? INT_MAX : l2->val;
90+
if (val1 <= val2) {
91+
p->next = l1;
92+
l1 = l1->next;
93+
} else {
94+
p->next = l2;
95+
l2 = l2->next;
96+
}
97+
}
98+
return dummy.next;
99+
}
100+
// 快慢指针找到中间节点
101+
static ListNode* findMiddle(ListNode* head) {
102+
if (head == nullptr) return nullptr;
103+
104+
ListNode *slow = head;
105+
ListNode *fast = head->next;
106+
107+
while (fast != nullptr && fast->next != nullptr) {
108+
slow = slow->next;
109+
fast = fast->next->next;
110+
}
111+
return slow;
112+
}
113+
};
114+
```
115+
{% endif %}
17116
18117
19118
### 相关题目

0 commit comments

Comments
 (0)