-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (63 loc) · 935 Bytes
/
main.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
package main
import "fmt"
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
type ListNode struct {
Val int
Next *ListNode
}
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
if list1 == nil {
return list2
}
if list2 == nil {
return list1
}
var (
node, result *ListNode
)
if list1.Val < list2.Val {
node = list1
result = mergeTwoLists(list1.Next, list2)
} else {
node = list2
result = mergeTwoLists(list2.Next, list1)
}
node.Next = result
return node
}
func main() {
list1 := &ListNode{
Val: 1,
Next: &ListNode{
Val: 2,
Next: &ListNode{
Val: 4,
Next: nil,
},
},
}
list2 := &ListNode{
Val: 1,
Next: &ListNode{
Val: 3,
Next: &ListNode{
Val: 4,
Next: nil,
},
},
}
v := mergeTwoLists(list1, list2)
for {
if v == nil {
break
}
fmt.Println(v.Val)
v = v.Next
}
}