Skip to content

Commit 5465bdf

Browse files
committed
up solution and test of problem middle of linkedlist
1 parent b78f8af commit 5465bdf

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, test } from 'bun:test';
2+
import { ListNode, middleNode } from './middle-of-linked-list';
3+
4+
describe('middleNode', () => {
5+
test('should return null for an empty list', () => {
6+
expect(middleNode(null)).toBeNull();
7+
});
8+
9+
test('should return the only node for a single-node list', () => {
10+
const head = new ListNode(1);
11+
expect(middleNode(head)).toBe(head);
12+
});
13+
14+
test('should return the middle node for an odd-length list', () => {
15+
const head = new ListNode(1, new ListNode(2, new ListNode(3)));
16+
expect(middleNode(head)).toBe(head.next); // Middle node is 2
17+
});
18+
19+
test('should return the second middle node for an even-length list', () => {
20+
const head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));
21+
expect(middleNode(head)).toBe(head?.next?.next ?? null); // Middle node is 3
22+
});
23+
24+
test('should return the middle node for a longer odd-length list', () => {
25+
const head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
26+
expect(middleNode(head)).toBe(head?.next?.next ?? null); // Middle node is 3
27+
});
28+
29+
test('should return the second middle node for a longer even-length list', () => {
30+
const head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6))))));
31+
expect(middleNode(head)).toBe(head?.next?.next?.next ?? null); // Middle node is 4
32+
});
33+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
export class ListNode {
14+
val: number
15+
next: ListNode | null
16+
constructor(val?: number, next?: ListNode | null) {
17+
this.val = (val === undefined ? 0 : val)
18+
this.next = (next === undefined ? null : next)
19+
}
20+
}
21+
22+
// Sử dụng kỹ thuật brute force O(1) and
23+
// Using two pointers technique
24+
export function middleNode(head: ListNode | null): ListNode | null {
25+
let fast = head; // O(1)
26+
let slow = head; // O(1)
27+
while (fast?.next) {
28+
slow = slow?.next ?? null;
29+
fast = fast?.next?.next ?? null
30+
}
31+
return slow;
32+
}; // Space: O(1), time (O(n/2))
33+
34+
// Space complexity O(1) Tại vì fast và slow chỉ lưu bộ nhớ!
35+
// console.log("middleNode", middleNode(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))))));
36+
console.log("middleNode", middleNode(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5, new ListNode(6))))))));

0 commit comments

Comments
 (0)