Skip to content

Commit c25f5c7

Browse files
committed
feat: add solutions to lc/lcof2 problem: Sort List
1 parent 86f4318 commit c25f5c7

File tree

12 files changed

+844
-1
lines changed

12 files changed

+844
-1
lines changed

lcof2/剑指 Offer II 077. 链表排序/README.md

Lines changed: 302 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,323 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
先用快慢指针找到链表中点,然后分成左右两个链表,递归排序左右链表。最后合并两个排序的链表即可。
62+
6163
<!-- tabs:start -->
6264

6365
### **Python3**
6466

6567
<!-- 这里可写当前语言的特殊实现逻辑 -->
6668

6769
```python
68-
70+
# Definition for singly-linked list.
71+
# class ListNode:
72+
# def __init__(self, val=0, next=None):
73+
# self.val = val
74+
# self.next = next
75+
class Solution:
76+
def sortList(self, head: ListNode) -> ListNode:
77+
if head is None or head.next is None:
78+
return head
79+
slow, fast = head, head.next
80+
while fast and fast.next:
81+
slow, fast = slow.next, fast.next.next
82+
t = slow.next
83+
slow.next = None
84+
l1, l2 = self.sortList(head), self.sortList(t)
85+
dummy = ListNode()
86+
cur = dummy
87+
while l1 and l2:
88+
if l1.val <= l2.val:
89+
cur.next = l1
90+
l1 = l1.next
91+
else:
92+
cur.next = l2
93+
l2 = l2.next
94+
cur = cur.next
95+
cur.next = l1 or l2
96+
return dummy.next
6997
```
7098

7199
### **Java**
72100

73101
<!-- 这里可写当前语言的特殊实现逻辑 -->
74102

75103
```java
104+
/**
105+
* Definition for singly-linked list.
106+
* public class ListNode {
107+
* int val;
108+
* ListNode next;
109+
* ListNode() {}
110+
* ListNode(int val) { this.val = val; }
111+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
112+
* }
113+
*/
114+
class Solution {
115+
public ListNode sortList(ListNode head) {
116+
if (head == null || head.next == null) {
117+
return head;
118+
}
119+
ListNode slow = head, fast = head.next;
120+
while (fast != null && fast.next != null) {
121+
slow = slow.next;
122+
fast = fast.next.next;
123+
}
124+
ListNode t = slow.next;
125+
slow.next = null;
126+
ListNode l1 = sortList(head);
127+
ListNode l2 = sortList(t);
128+
ListNode dummy = new ListNode();
129+
ListNode cur = dummy;
130+
while (l1 != null && l2 != null) {
131+
if (l1.val <= l2.val) {
132+
cur.next = l1;
133+
l1 = l1.next;
134+
} else {
135+
cur.next = l2;
136+
l2 = l2.next;
137+
}
138+
cur = cur.next;
139+
}
140+
cur.next = l1 == null ? l2 : l1;
141+
return dummy.next;
142+
}
143+
}
144+
```
145+
146+
### **C++**
147+
148+
```cpp
149+
/**
150+
* Definition for singly-linked list.
151+
* struct ListNode {
152+
* int val;
153+
* ListNode *next;
154+
* ListNode() : val(0), next(nullptr) {}
155+
* ListNode(int x) : val(x), next(nullptr) {}
156+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
157+
* };
158+
*/
159+
class Solution {
160+
public:
161+
ListNode* sortList(ListNode* head) {
162+
if (!head || !head->next) return head;
163+
auto* slow = head;
164+
auto* fast = head->next;
165+
while (fast && fast->next)
166+
{
167+
slow = slow->next;
168+
fast = fast->next->next;
169+
}
170+
auto* t = slow->next;
171+
slow->next = nullptr;
172+
auto* l1 = sortList(head);
173+
auto* l2 = sortList(t);
174+
auto* dummy = new ListNode();
175+
auto* cur = dummy;
176+
while (l1 && l2)
177+
{
178+
if (l1->val <= l2->val)
179+
{
180+
cur->next = l1;
181+
l1 = l1->next;
182+
}
183+
else
184+
{
185+
cur->next = l2;
186+
l2 = l2->next;
187+
}
188+
cur = cur->next;
189+
}
190+
cur->next = l1 ? l1 : l2;
191+
return dummy->next;
192+
}
193+
};
194+
```
195+
196+
### **Go**
197+
198+
```go
199+
/**
200+
* Definition for singly-linked list.
201+
* type ListNode struct {
202+
* Val int
203+
* Next *ListNode
204+
* }
205+
*/
206+
func sortList(head *ListNode) *ListNode {
207+
if head == nil || head.Next == nil {
208+
return head
209+
}
210+
slow, fast := head, head.Next
211+
for fast != nil && fast.Next != nil {
212+
slow, fast = slow.Next, fast.Next.Next
213+
}
214+
t := slow.Next
215+
slow.Next = nil
216+
l1, l2 := sortList(head), sortList(t)
217+
dummy := &ListNode{}
218+
cur := dummy
219+
for l1 != nil && l2 != nil {
220+
if l1.Val <= l2.Val {
221+
cur.Next = l1
222+
l1 = l1.Next
223+
} else {
224+
cur.Next = l2
225+
l2 = l2.Next
226+
}
227+
cur = cur.Next
228+
}
229+
if l1 != nil {
230+
cur.Next = l1
231+
} else {
232+
cur.Next = l2
233+
}
234+
return dummy.Next
235+
}
236+
```
237+
238+
### **JavaScript**
239+
240+
```js
241+
/**
242+
* Definition for singly-linked list.
243+
* function ListNode(val, next) {
244+
* this.val = (val===undefined ? 0 : val)
245+
* this.next = (next===undefined ? null : next)
246+
* }
247+
*/
248+
/**
249+
* @param {ListNode} head
250+
* @return {ListNode}
251+
*/
252+
var sortList = function(head) {
253+
if (!head || !head.next) {
254+
return head;
255+
}
256+
let slow = head;
257+
let fast = head.next;
258+
while (fast && fast.next) {
259+
slow = slow.next;
260+
fast = fast.next.next;
261+
}
262+
let t = slow.next;
263+
slow.next = null;
264+
let l1 = sortList(head);
265+
let l2 = sortList(t);
266+
const dummy = new ListNode();
267+
let cur = dummy;
268+
while (l1 && l2) {
269+
if (l1.val <= l2.val) {
270+
cur.next = l1;
271+
l1 = l1.next;
272+
} else {
273+
cur.next = l2;
274+
l2 = l2.next;
275+
}
276+
cur = cur.next;
277+
}
278+
cur.next = l1 || l2;
279+
return dummy.next;
280+
};
281+
```
282+
283+
### **C#**
284+
285+
```cs
286+
/**
287+
* Definition for singly-linked list.
288+
* public class ListNode {
289+
* public int val;
290+
* public ListNode next;
291+
* public ListNode(int val=0, ListNode next=null) {
292+
* this.val = val;
293+
* this.next = next;
294+
* }
295+
* }
296+
*/
297+
public class Solution {
298+
public ListNode SortList(ListNode head) {
299+
if (head == null || head.next == null)
300+
{
301+
return head;
302+
}
303+
ListNode slow = head, fast = head.next;
304+
while (fast != null && fast.next != null)
305+
{
306+
slow = slow.next;
307+
fast = fast.next.next;
308+
}
309+
ListNode t = slow.next;
310+
slow.next = null;
311+
ListNode l1 = SortList(head);
312+
ListNode l2 = SortList(t);
313+
ListNode dummy = new ListNode();
314+
ListNode cur = dummy;
315+
while (l1 != null && l2 != null)
316+
{
317+
if (l1.val <= l2.val)
318+
{
319+
cur.next = l1;
320+
l1 = l1.next;
321+
}
322+
else
323+
{
324+
cur.next = l2;
325+
l2 = l2.next;
326+
}
327+
cur = cur.next;
328+
}
329+
cur.next = l1 == null ? l2 : l1;
330+
return dummy.next;
331+
}
332+
}
333+
```
76334

335+
### **TypeScript**
336+
337+
```ts
338+
/**
339+
* Definition for singly-linked list.
340+
* class ListNode {
341+
* val: number
342+
* next: ListNode | null
343+
* constructor(val?: number, next?: ListNode | null) {
344+
* this.val = (val===undefined ? 0 : val)
345+
* this.next = (next===undefined ? null : next)
346+
* }
347+
* }
348+
*/
349+
350+
function sortList(head: ListNode | null): ListNode | null {
351+
if (head == null || head.next == null) return head;
352+
// 快慢指针定位中点
353+
let slow: ListNode = head, fast: ListNode = head.next;
354+
while (fast != null && fast.next != null) {
355+
slow = slow.next;
356+
fast = fast.next.next;
357+
}
358+
// 归并排序
359+
let mid: ListNode = slow.next;
360+
slow.next = null;
361+
let l1: ListNode = sortList(head);
362+
let l2: ListNode = sortList(mid);
363+
let dummy: ListNode = new ListNode();
364+
let cur: ListNode = dummy;
365+
while (l1 != null && l2 != null) {
366+
if (l1.val <= l2.val) {
367+
cur.next = l1;
368+
l1 = l1.next;
369+
} else {
370+
cur.next = l2;
371+
l2 = l2.next;
372+
}
373+
cur = cur.next;
374+
}
375+
cur.next = l1 == null ? l2 : l1;
376+
return dummy.next;
377+
};
77378
```
78379

79380
### **...**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* sortList(ListNode* head) {
14+
if (!head || !head->next) return head;
15+
auto* slow = head;
16+
auto* fast = head->next;
17+
while (fast && fast->next)
18+
{
19+
slow = slow->next;
20+
fast = fast->next->next;
21+
}
22+
auto* t = slow->next;
23+
slow->next = nullptr;
24+
auto* l1 = sortList(head);
25+
auto* l2 = sortList(t);
26+
auto* dummy = new ListNode();
27+
auto* cur = dummy;
28+
while (l1 && l2)
29+
{
30+
if (l1->val <= l2->val)
31+
{
32+
cur->next = l1;
33+
l1 = l1->next;
34+
}
35+
else
36+
{
37+
cur->next = l2;
38+
l2 = l2->next;
39+
}
40+
cur = cur->next;
41+
}
42+
cur->next = l1 ? l1 : l2;
43+
return dummy->next;
44+
}
45+
};

0 commit comments

Comments
 (0)