-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy path148.排序链表.go
80 lines (79 loc) · 1.48 KB
/
148.排序链表.go
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
76
77
78
79
80
/*
* @lc app=leetcode.cn id=148 lang=golang
*
* [148] 排序链表
*
* https://leetcode-cn.com/problems/sort-list/description/
*
* algorithms
* Medium (60.39%)
* Likes: 194
* Dislikes: 0
* Total Accepted: 13.4K
* Total Submissions: 22.3K
* Testcase Example: '[4,2,1,3]'
*
* 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
*
* 示例 1:
*
* 输入: 4->2->1->3
* 输出: 1->2->3->4
*
*
* 示例 2:
*
* 输入: -1->5->3->4->0
* 输出: -1->0->3->4->5
*
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// 采用非原地快排
func sortList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
sl := &ListNode{}
ll := &ListNode{}
sn := sl // 小端当前节点
ln := ll // 大端当前节点
// 单次比较,比较基准取头节点
cur := head.Next // 注意基准要单独出来,否则不能满足一个元素的退出条件
for cur != nil {
if cur.Val < head.Val {
sn.Next = cur
sn = cur
} else {
ln.Next = cur
ln = cur
}
cur = cur.Next
}
// 切断原链表
sn.Next = nil
ln.Next = nil
// 递归快排
sl = sortList(sl.Next)
ll = sortList(ll.Next)
// 合并返回新队列
cur = sl
if cur != nil {
for cur.Next != nil {
cur = cur.Next
}
cur.Next = head
head.Next = nil // 注意要切断原head
if ll != nil {
head.Next = ll
}
return sl
}
head.Next = ll
return head
}