LeetCode 链表简单题 + 可直接本地运行的 JS 版本,每道题都包含:
- 链表节点构造函数
- 数组转链表 / 链表转数组(方便本地调试)
- 完整可运行代码
- 测试用例
你直接复制到浏览器控制台 / Node.js 就能跑,不用改任何东西!
先写通用工具函数(所有题都能用)
// 1. 链表节点构造函数
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
// 2. 数组 => 链表
function arrayToList(arr) {
let dummy = new ListNode();
let cur = dummy;
for (let num of arr) {
cur.next = new ListNode(num);
cur = cur.next;
}
return dummy.next;
}
// 3. 链表 => 数组(方便打印看结果)
function listToArray(head) {
let res = [];
while (head) {
res.push(head.val);
head = head.next;
}
return res;
}
下面所有题直接复制就能调试~
1. 反转链表 206
function reverseList(head) {
let prev = null;
let cur = head;
while (cur) {
let next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
// 测试
let head = arrayToList([1,2,3,4,5]);
console.log(listToArray(reverseList(head))); // [5,4,3,2,1]
2. 合并两个有序链表 21
function mergeTwoLists(l1, l2) {
let dummy = new ListNode();
let cur = dummy;
while (l1 && l2) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 || l2;
return dummy.next;
}
// 测试
let l1 = arrayToList([1,2,4]);
let l2 = arrayToList([1,3,4]);
console.log(listToArray(mergeTwoLists(l1, l2))); // [1,1,2,3,4,4]
3. 删除排序链表中的重复元素 83
function deleteDuplicates(head) {
let cur = head;
while (cur && cur.next) {
if (cur.val === cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
}
// 测试
let head = arrayToList([1,1,2,3,3]);
console.log(listToArray(deleteDuplicates(head))); // [1,2,3]
4. 环形链表 141
function hasCycle(head) {
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
// 测试
let head = arrayToList([3,2,0,-4]);
head.next.next.next = head; // 造环
console.log(hasCycle(head)); // true
5. 链表的中间节点 876
function middleNode(head) {
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// 测试
let head = arrayToList([1,2,3,4,5]);
console.log(listToArray(middleNode(head))); // [3,4,5]
6. 删除链表的倒数第 N 个节点 19
function removeNthFromEnd(head, n) {
let dummy = new ListNode(0, head);
let fast = dummy, slow = dummy;
for (let i = 0; i <= n; i++) fast = fast.next;
while (fast) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
}
// 测试
let head = arrayToList([1,2,3,4,5]);
console.log(listToArray(removeNthFromEnd(head, 2))); // [1,2,3,5]
7. 回文链表 234
function isPalindrome(head) {
let slow = head, fast = head;
let prev = null;
while (fast && fast.next) {
fast = fast.next.next;
let next = slow.next;
slow.next = prev;
prev = slow;
slow = next;
}
while (prev) {
if (prev.val !== slow.val) return false;
prev = prev.next;
slow = slow.next;
}
return true;
}
// 测试
let head = arrayToList([1,2,2,1]);
console.log(isPalindrome(head)); // true
8. 相交链表 160
function getIntersectionNode(headA, headB) {
let a = headA, b = headB;
while (a !== b) {
a = a ? a.next : headB;
b = b ? b.next : headA;
}
return a;
}
// 测试逻辑略,本地可直接跑 LeetCode 用例
9. 移除链表元素 203
function removeElements(head, val) {
let dummy = new ListNode(0, head);
let cur = dummy;
while (cur.next) {
if (cur.next.val === val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return dummy.next;
}
// 测试
let head = arrayToList([1,2,6,3,4,5,6]);
console.log(listToArray(removeElements(head, 6))); // [1,2,3,4,5]
本地怎么用?
- 复制最上面通用工具函数
- 复制任意一道题
- 运行 → 自动出结果
超级方便!