-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeTwoSortedLists.js
39 lines (37 loc) · 967 Bytes
/
MergeTwoSortedLists.js
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
/**
* Variables:
* prehead: dummy listNode that holds sorted combined linked lists
* curr: pointer for prehead
* Loop while l1 and l2 are not null
* - if l1's value is less than or equal to l2's value
* - have curr's next pointer equal to l1
* - move l1 forward
* - else
* - have curr's next pointer equal to l2
* - move l2 forward
* - move curr forward
* Have curr's next pointer equal to either l1 or l2 (whichever one has remaining nodes)
* Return prehead's next pointer
*/
const mergeTwoLists = (l1, l2) => {
let prehead = new ListNode();
let curr = prehead;
while (l1 && l2) {
if (l1.val <= l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
curr.next = l1 ? l1 : l2;
return prehead.next;
};
/**
* N - number of nodes in l1
* M - number of nodes in l2
* Time Complexity: O(N+M)
* Space Complexity: O(1)
*/