Skip to content

Commit 2b4b1dc

Browse files
committed
leetcode: up exercise merge two sort linkedlist
1 parent a68a8ce commit 2b4b1dc

File tree

2 files changed

+68
-25
lines changed

2 files changed

+68
-25
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// ... existing code ...
2+
import { describe, it, expect } from "bun:test";
3+
import { mergeTwoLists, ListNode } from "./merge-two-lists";
4+
5+
// Unit tests for mergeTwoLists function
6+
describe('mergeTwoLists', () => {
7+
it('should return null when both lists are empty', () => {
8+
expect(mergeTwoLists(null, null)).toBeNull();
9+
});
10+
11+
it('should return the non-empty list when one list is empty', () => {
12+
const list1 = new ListNode(1, new ListNode(2));
13+
expect(mergeTwoLists(list1, null)).toEqual(list1);
14+
expect(mergeTwoLists(null, list1)).toEqual(list1);
15+
});
16+
17+
it('should merge two sorted lists', () => {
18+
const list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
19+
const list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
20+
const mergedList = mergeTwoLists(list1, list2);
21+
22+
expect(mergedList).toEqual(new ListNode(1, new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(4)))))));
23+
});
24+
25+
it('should handle lists with duplicate values', () => {
26+
const list1 = new ListNode(1, new ListNode(1));
27+
const list2 = new ListNode(1, new ListNode(1));
28+
const mergedList = mergeTwoLists(list1, list2);
29+
30+
expect(mergedList).toEqual(new ListNode(1, new ListNode(1, new ListNode(1, new ListNode(1)))));
31+
});
32+
33+
it('should handle lists with different lengths', () => {
34+
const list1 = new ListNode(1, new ListNode(3));
35+
const list2 = new ListNode(2);
36+
const mergedList = mergeTwoLists(list1, list2);
37+
38+
expect(mergedList).toEqual(new ListNode(1, new ListNode(2, new ListNode(3))));
39+
});
40+
});
41+
42+
// ... existing code ...

src/leetcode/linked_list/merge-two-lists.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* }
1111
*/
1212

13-
class ListNode {
13+
export class ListNode {
1414
val: number
1515
next: ListNode | null
1616
constructor(val?: number, next?: ListNode | null) {
@@ -19,37 +19,38 @@ class ListNode {
1919
}
2020
}
2121

22-
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
22+
// Time complexity O(n + m), n: list1, m: list2
23+
export function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
24+
if (list1 == null) return list2
25+
if (list2 == null) return list1
2326

24-
let firstItemList1 = list1
25-
let firstItemList2 = list2
2627

2728
let node: ListNode | null = {
2829
val: 0,
2930
next: null
3031
}
32+
// Gán giá trị đầu tiên
33+
if (list1.val > list2.val) {
34+
node = new ListNode(list2.val)
35+
list2 = list2.next
36+
} else {
37+
node = new ListNode(list1.val)
38+
list1 = list1.next
39+
}
40+
let pointer = node
3141

32-
let temp: any = null
33-
let holder: any = node
34-
while ((firstItemList1 as any).next && (firstItemList2 as any).next) {
35-
if (firstItemList1 != undefined && firstItemList1.val < (firstItemList2 as any).val) {
36-
holder.val = firstItemList1.val
37-
firstItemList1 = firstItemList1.next
38-
if (temp) {
39-
holder.next = temp
40-
}
41-
temp = holder
42+
// So sánh với các giá trị tiếp theo. Liên quan đến bộ nhớ của LinkedList nữa!
43+
while (list1 || list2) {
44+
if ((list1 != null && list2 != null && (list1.val < list2.val)) || list2 == null) {
45+
const newNode = new ListNode(list1?.val)
46+
pointer.next = newNode
47+
pointer = newNode
48+
list1 = list1?.next ?? null
4249
} else {
43-
holder.val = firstItemList2?.val ?? 0
44-
firstItemList2 = firstItemList2?.next ?? null
45-
if (temp) {
46-
holder.next = temp
47-
}
48-
temp = holder
49-
}
50-
holder.next = {
51-
val: 0,
52-
next: null
50+
const newNode = new ListNode(list2?.val)
51+
pointer.next = newNode
52+
pointer = newNode
53+
list2 = list2?.next ?? null
5354
}
5455
}
5556
return node
@@ -58,4 +59,4 @@ function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode
5859

5960
const list1 = new ListNode(1, new ListNode(2, new ListNode(4, null)))
6061
const list2 = new ListNode(1, new ListNode(3, new ListNode(4, null)))
61-
mergeTwoLists(list1, list2)
62+
console.log("mergeTwoLists(list1, list2)", mergeTwoLists(list1, list2));

0 commit comments

Comments
 (0)