-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_two_sorted_lists.go
58 lines (52 loc) · 1.15 KB
/
merge_two_sorted_lists.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
package leetcode
// Definition for singly-linked list.
type ListNode struct {
Val int
Next *ListNode
}
// Time complexity: O(n + m) where n and m are the length of list1 and list2
// Space complexity: O(n + m)
// Recursive approach
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
return merge(list1, list2, &ListNode{0, nil}).Next
}
func merge(l1, l2, res *ListNode) *ListNode {
if l1 == nil {
res.Next = l2
} else if l2 == nil {
res.Next = l1
} else {
if l1.Val <= l2.Val {
res.Next = l1
l1 = l1.Next
} else {
res.Next = l2
l2 = l2.Next
}
merge(l1, l2, res.Next)
}
return res
}
// Time complexity: O(n + m) where n and m are the length of list1 and list2
// Space complexity: O(1)
// Iterative approach
func mergeTwoLists2(list1 *ListNode, list2 *ListNode) *ListNode {
dummyHead := &ListNode{0, nil}
curr := dummyHead
for list1 != nil && list2 != nil {
if list1.Val <= list2.Val {
curr.Next = list1
list1 = list1.Next
} else {
curr.Next = list2
list2 = list2.Next
}
curr = curr.Next
}
if list1 != nil {
curr.Next = list1
} else if list2 != nil {
curr.Next = list2
}
return dummyHead.Next
}