-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-mergeTwoLists.go
79 lines (74 loc) · 1.17 KB
/
21-mergeTwoLists.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
// https://leetcode-cn.com/problems/merge-two-sorted-lists/
package main
import (
"fmt"
"leetcode/util"
)
type ListNode = util.ListNode
/**
* 时间 O(m+n) m: l1 长度 n: l2长度
* 空间 O(1)
*/
func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
var root, cur, tmp *ListNode
for l1 != nil && l2 != nil {
if l1.Val >= l2.Val {
tmp = l2
l2 = l2.Next
} else {
tmp = l1
l1 = l1.Next
}
if root == nil {
root = tmp
cur = root
} else {
cur.Next = tmp
cur = cur.Next
}
}
if l1 != nil {
cur.Next = l1
}
if l2 != nil {
cur.Next = l2
}
return root
}
/**
* 优雅地解决头为 nil 判断问题。代码更简洁
* 时间 O(m+n) m: l1 长度 n: l2长度
* 空间 O(1)
*/
func mergeTwoLists2(l1 *ListNode, l2 *ListNode) *ListNode {
root := new(ListNode)
cur := root
for l1 != nil && l2 != nil {
var tmp *ListNode
if l1.Val >= l2.Val {
tmp = l2
l2 = l2.Next
} else {
tmp = l1
l1 = l1.Next
}
cur.Next = tmp
cur = cur.Next
}
if l1 != nil {
cur.Next = l1
}
if l2 != nil {
cur.Next = l2
}
return root.Next
}
func main() {
fmt.Println("vim-go")
}