Skip to content

Commit fd0b16b

Browse files
committed
Solve problem: Merge Two Sorted Lists
1 parent a29b2b2 commit fd0b16b

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

README.MD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LEETCODE PATTERNS
22

3-
Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetcode-patterns/) website
3+
Solving problems with LeetCode
44

55
## Problems
66

@@ -25,3 +25,4 @@ Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetc
2525
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |
2626
| [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isSubsequence/isSubsequence.js) | Easy |
2727
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |
28+
| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/mergeTwoLists/mergeTwoLists.js) | Easy |

helpers/createLinkedListFromArray.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function createLinkedListFromArray(arr) {
2+
if (!arr || arr.length === 0) {
3+
return null;
4+
}
5+
6+
let head = {
7+
val: arr[0],
8+
next: null,
9+
};
10+
let current = head;
11+
12+
for (let i = 1; i < arr.length; i++) {
13+
let newNode = {
14+
val: arr[i],
15+
next: null,
16+
};
17+
current.next = newNode;
18+
current = newNode;
19+
}
20+
21+
return head;
22+
}
23+
24+
module.exports = createLinkedListFromArray;

solutions/isIsomorphic/isIsomorphic.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const isIsomorphic = require('./isIsomorphic');
22

3-
describe('isIsomorphic', () => {
3+
describe('Isomorphic Strings', () => {
44
it('should return true when s and t are empty strings', () => {
55
const result = isIsomorphic('', '');
66
expect(result).toBe(true);

solutions/mergeTwoLists/README.MD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [21. Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/description/)
2+
3+
You are given the heads of two sorted linked lists `list1` and `list2`.
4+
5+
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
6+
7+
Return the head of the merged linked list.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {ListNode} list1
3+
* @param {ListNode} list2
4+
* @return {ListNode}
5+
*/
6+
function mergeTwoLists(list1, list2) {
7+
if (!list1) return list2;
8+
if (!list2) return list1;
9+
10+
const result = {};
11+
12+
if (list1.val < list2.val) {
13+
result.val = list1.val;
14+
result.next = mergeTwoLists(list1.next, list2);
15+
} else {
16+
result.val = list2.val;
17+
result.next = mergeTwoLists(list1, list2.next);
18+
}
19+
20+
return result;
21+
}
22+
23+
module.exports = mergeTwoLists;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const createLinkedListFromArray = require('../../helpers/createLinkedListFromArray');
2+
const mergeTwoLists = require('./mergeTwoLists');
3+
4+
describe('Merge Two Sorted Lists', () => {
5+
it('should return null when both input lists are empty', () => {
6+
const result = mergeTwoLists(null, null);
7+
expect(result).toBeNull();
8+
});
9+
10+
it('should return the non-empty list when one input list is empty', () => {
11+
const l1 = createLinkedListFromArray([1, 2, 3]);
12+
const result1 = mergeTwoLists(l1, null);
13+
expect(result1).toEqual(l1);
14+
15+
const l2 = createLinkedListFromArray([4, 5, 6]);
16+
const result2 = mergeTwoLists(null, l2);
17+
expect(result2).toEqual(l2);
18+
});
19+
20+
it('should return the merged list when both input lists are non-empty and sorted', () => {
21+
const l1 = createLinkedListFromArray([1, 3, 5]);
22+
const l2 = createLinkedListFromArray([2, 4, 6]);
23+
const expected = createLinkedListFromArray([1, 2, 3, 4, 5, 6]);
24+
25+
const result = mergeTwoLists(l1, l2);
26+
expect(result).toEqual(expected);
27+
});
28+
29+
it('should handle edge cases where the input lists contain duplicate values', () => {
30+
const l1 = createLinkedListFromArray([1, 2, 2, 3, 4, 5]);
31+
const l2 = createLinkedListFromArray([1, 3, 4, 5, 6, 6]);
32+
const expected = createLinkedListFromArray([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]);
33+
34+
const result = mergeTwoLists(l1, l2);
35+
expect(result).toEqual(expected);
36+
});
37+
38+
it('should handle edge cases where the input lists have different lengths', () => {
39+
const l1 = createLinkedListFromArray([1, 3, 5]);
40+
const l2 = createLinkedListFromArray([2, 4]);
41+
const expected = createLinkedListFromArray([1, 2, 3, 4, 5]);
42+
43+
const result = mergeTwoLists(l1, l2);
44+
expect(result).toEqual(expected);
45+
});
46+
});

0 commit comments

Comments
 (0)