-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path147.go
More file actions
37 lines (35 loc) · 659 Bytes
/
147.go
File metadata and controls
37 lines (35 loc) · 659 Bytes
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
package main
import "math"
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func insertionSortList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
preHead := &ListNode{Val: math.MaxInt64}
preHead.Next = head
cur, next := head, head.Next
for next != nil {
if next.Val > cur.Val {
cur = next
next = next.Next
continue
}
// 断开
cur.Next = next.Next
pre1, pre2 := preHead, preHead.Next
for next.Val > pre2.Val {
pre1 = pre2
pre2 = pre2.Next
}
pre1.Next = next
next.Next = pre2
next = cur.Next
}
return preHead.Next
}