-
Notifications
You must be signed in to change notification settings - Fork 7
/
merge_list.go
52 lines (50 loc) · 845 Bytes
/
merge_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
package main
/*
* type ListNode struct{
* Val int
* Next *ListNode
* }
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
// https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&tqId=11169&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking
func Merge(pHead1 *ListNode, pHead2 *ListNode) *ListNode {
if pHead1 == nil {
return pHead2
}
if pHead2 == nil {
return pHead1
}
p := pHead1
q := pHead2
var root, t *ListNode
for p != nil && q != nil {
var n *ListNode
if p.Val < q.Val {
n = p
p = p.Next
} else {
n = q
q = q.Next
}
n.Next = nil
if root == nil {
root = n
t = n
} else {
t.Next = n
t = n
}
}
if p != nil {
t.Next = p
}
if q != nil {
t.Next = q
}
return root
}