链表题为什么不容易写对? 反转链表、合并有序链表这类简单题,在面试中能写对的人寥寥。

难点

被指针的 next 绕晕,next.next 等

for (cur.next && cur.next.next) {
    first := cur.next;
    second := cur.next.next;
    first.next = second.next;
    cur.next = second;
    cur.next.next = first;
    cur = cur.next.next;
 }

四大要点

理解指针的含义

改变引用前,记得缓存

保护节点的使用(fakeHead)

检查边界条件(数组同理)

与数组对比

LeetCode 实战

LeetBook:https://leetcode.cn/leetbook/detail/linked-list/

讲过的题