-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
MergeTwoSortedLinkedLists.js
45 lines (40 loc) · 1.2 KB
/
MergeTwoSortedLinkedLists.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
40
41
42
43
44
45
import { LinkedList } from './SinglyLinkedList.js'
/**
* A LinkedList-based solution for merging two sorted linked lists into one sorted list.
*
* @param {LinkedList} list1 - The the first sorted linked list.
* @param {LinkedList} list2 - The second sorted linked list.
* @returns {LinkedList} - The merged sorted linked list.
*
* @example
* const list1 = new LinkedList([1,2,4]);
*
* const list2 = new LinkedList([1,3,4]);
*
* const result = mergeLinkedLists(list1, list2);
* // Returns the merged linked list representing 1 -> 1 -> 2 -> 3 -> 4 -> 4
*/
function mergeLinkedLists(list1, list2) {
const mergedList = new LinkedList()
let current1 = list1.headNode
let current2 = list2.headNode
while (current1 || current2) {
if (!current1) {
mergedList.addLast(current2.data)
current2 = current2.next
} else if (!current2) {
mergedList.addLast(current1.data)
current1 = current1.next
} else {
if (current1.data < current2.data) {
mergedList.addLast(current1.data)
current1 = current1.next
} else {
mergedList.addLast(current2.data)
current2 = current2.next
}
}
}
return mergedList
}
export { mergeLinkedLists }