-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_list.go
74 lines (63 loc) · 1.31 KB
/
sort_list.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
package problem0148
import (
. "leetcodedaily/helpers/listnode"
"sort"
)
/*
Given the head of a linked list, return the list after sorting it in ascending order.
*/
func sortList(head *ListNode) *ListNode {
var res []int
var temp = head
for temp != nil {
res = append(res, temp.Val)
temp = temp.Next
}
sort.Ints(res)
temp = head
for i := range res {
temp.Val = res[i]
temp = temp.Next
}
return head
}
func sortListInPlace(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
// Splitting the list into two using slow and fast approach
var temp, slow, fast *ListNode = nil, head, head
for fast != nil && fast.Next != nil {
temp = slow
slow = slow.Next
fast = fast.Next.Next
}
temp.Next = nil
// Sorting each half
l1, l2 := sortListInPlace(head), sortListInPlace(slow)
// Merging them
return mergeList(l1, l2)
}
func mergeList(l1, l2 *ListNode) *ListNode {
var ptr = &ListNode{}
var cur = ptr
// Looping over the lists, and merging them into ptr
// in ascending order
for l1 != nil && l2 != nil {
if l1.Val <= l2.Val {
cur.Next = l1
l1 = l1.Next
} else {
cur.Next = l2
l2 = l2.Next
}
cur = cur.Next
}
// If lists aren't equal size, add the rest
if l1 != nil {
cur.Next = l1
} else if l2 != nil {
cur.Next = l2
}
return ptr.Next
}