Skip to content

Commit af4a2c7

Browse files
authored
Merge pull request #2467 from SamTheKorean/main
[SAM] Week 4 solutions
2 parents 80a15c7 + 7f9334a commit af4a2c7

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// TC : O(n) n being the length of list
2+
// SC : O(n) n being the size of the two list
3+
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
4+
// Create a dummy node to simplify edge cases
5+
dummy := &ListNode{}
6+
current := dummy
7+
8+
// Merge lists
9+
for list1 != nil && list2 != nil {
10+
if list1.Val < list2.Val {
11+
current.Next = list1
12+
list1 = list1.Next
13+
} else {
14+
current.Next = list2
15+
list2 = list2.Next
16+
}
17+
current = current.Next
18+
}
19+
20+
if list1 != nil {
21+
current.Next = list1
22+
} else {
23+
current.Next = list2
24+
}
25+
26+
return dummy.Next
27+
}

0 commit comments

Comments
 (0)