Skip to content

Commit fb172a9

Browse files
committed
feat: add mergeTwoSortedLinkedLIsts algorithms
1 parent 1de5ab7 commit fb172a9

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Node } from './SinglyLinkedList.js'
2+
/**
3+
* A LinkedList-based solution for merging two sorted linked lists into one sorted list.
4+
*
5+
* @param {Node} list1 - The head of the first sorted linked list.
6+
* @param {Node} list2 - The head of the second sorted linked list.
7+
* @returns {Node} - The head of the merged sorted linked list.
8+
*
9+
* @example
10+
* const list1 = new ListNode(1);
11+
* list1.next = new ListNode(2);
12+
* list1.next.next = new ListNode(4);
13+
*
14+
* const list2 = new ListNode(1);
15+
* list2.next = new ListNode(3);
16+
* list2.next.next = new ListNode(4);
17+
*
18+
* const result = mergeLists(list1, list2);
19+
* // Returns the head of a linked list representing 1 -> 1 -> 2 -> 3 -> 4 -> 4
20+
*/
21+
22+
class MergeTwoSortedLinkedLists {
23+
mergeLists(list1, list2) {
24+
let dummy = new Node(-1)
25+
let current = dummy
26+
27+
while (list1 && list2) {
28+
if (list1.val < list2.val) {
29+
current.next = list1
30+
list1 = list1.next
31+
} else {
32+
current.next = list2
33+
list2 = list2.next
34+
}
35+
current = current.next
36+
}
37+
38+
// If one of the lists is not empty, append it to the result
39+
if (list1) {
40+
current.next = list1
41+
} else if (list2) {
42+
current.next = list2
43+
}
44+
45+
return dummy.next
46+
}
47+
48+
/**
49+
* Converts a linked list to an array.
50+
*
51+
* @param {Node} head - The head of the linked list.
52+
* @returns {number[]} - An array representing the linked list values.
53+
*/
54+
linkedListToArray(head) {
55+
const resultArray = []
56+
while (head) {
57+
resultArray.push(head.val)
58+
head = head.next
59+
}
60+
return resultArray
61+
}
62+
}
63+
64+
export { MergeTwoSortedLinkedLists }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { MergeTwoSortedLinkedLists } from '../MergeTwoSortedLinkedLists.js'
2+
import { Node } from '../SinglyLinkedList.js'
3+
4+
describe('MergeTwoSortedLinkedLists', () => {
5+
it('Merges two sorted linked lists', () => {
6+
const list1 = new Node(1)
7+
list1.next = new Node(2)
8+
list1.next.next = new Node(4)
9+
10+
const list2 = new Node(1)
11+
list2.next = new Node(3)
12+
list2.next.next = new Node(4)
13+
14+
const expectedResult = new Node(1)
15+
expectedResult.next = new Node(1)
16+
expectedResult.next.next = new Node(2)
17+
expectedResult.next.next.next = new Node(3)
18+
expectedResult.next.next.next.next = new Node(4)
19+
expectedResult.next.next.next.next.next = new Node(4)
20+
21+
const merger = new MergeTwoSortedLinkedLists()
22+
const result = merger.mergeLists(list1, list2)
23+
24+
expect(merger.linkedListToArray(result)).toEqual(
25+
merger.linkedListToArray(expectedResult)
26+
)
27+
})
28+
29+
it('Merges two empty linked lists', () => {
30+
const list1 = null
31+
const list2 = null
32+
33+
const expectedResult = null
34+
35+
const merger = new MergeTwoSortedLinkedLists()
36+
const result = merger.mergeLists(list1, list2)
37+
38+
expect(result).toEqual(expectedResult)
39+
})
40+
41+
it('Merges one empty linked list with a non-empty one', () => {
42+
const list1 = null
43+
const list2 = new Node(0)
44+
45+
const expectedResult = new Node(0)
46+
47+
const merger = new MergeTwoSortedLinkedLists()
48+
const result = merger.mergeLists(list1, list2)
49+
50+
expect(merger.linkedListToArray(result)).toEqual(
51+
merger.linkedListToArray(expectedResult)
52+
)
53+
})
54+
})

0 commit comments

Comments
 (0)