LeetCode 链表

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]

本地怎么用?

  1. 复制最上面通用工具函数
  2. 复制任意一道题
  3. 运行 → 自动出结果

超级方便!

相关推荐
神仙别闹1 小时前
VUE框架 + Element UI + Node 模拟打印机的 Web 即时打印
前端·vue.js·ui
vivo互联网技术1 小时前
把输入框变成 AI 的“超级入口”(ProseMirror 全流程实战)
前端·agent
lunzi_08261 小时前
《图解HTTP》--第5章-与HTTP协作的Web服务器
服务器·前端·http
李剑一1 小时前
面试第一关!面试官:讲一下事件循环机制,宏&微任务,还有渲染时机
前端·面试
shuoshuohaohao1 小时前
《CSS》
前端·css
西部荒野子1 小时前
Zustand 状态管理规范:别让轻量状态变成隐形通知风暴
前端·javascript
之歆1 小时前
Day03_ES6 深度解析与实战应用:运算符、Symbol、Class、集合与迭代协议
前端·ecmascript·es6
Carson带你学Android1 小时前
Kotlin放大招!官方 Skills 直接喂出「专家级」代码
android·前端·kotlin