We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 80a15c7 + 7f9334a commit af4a2c7Copy full SHA for af4a2c7
1 file changed
merge-two-sorted-lists/samthekorean.go
@@ -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
22
23
24
25
26
+ return dummy.Next
27
+}
0 commit comments