19. Remove Nth Node From End of List
- 建立虚拟头节点(因为可能删除头节点), first, second 指向dummy(虚拟头节点)
- first指针向后走n步, 然后 first, second同时向后走, 直到 first到达最后一个元素
1 | /** |
237. Delete Node in a Linked List
##
1 |
|
##
也可以缩成一句话 *(node) = *(node->next); 结构体的等号就是直接复制
[83. Remove Duplicates from Sorted List] (https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
1 |
61. Rotate List
与19题一样,使用了双指针
1 |
|
[24. Swap Nodes in Pairs] (https://leetcode.com/problems/swap-nodes-in-pairs/)
由于头节点会变, 所以要设置一个虚拟头节点
1 | /** |

按顺序做完以上三步后, 更新 p 指针 指向 a的位置
[206. Reverse Linked List] (https://leetcode.com/problems/reverse-linked-list/)
1 | /** |
92. Reverse Linked List II
反转 m ~ n 之间的链表
1 | class Solution { |
160. Intersection of Two Linked Lists
神奇的做法:
如果 a != b 那么二者在交点相遇; 反之 二者走完 a+b+c 步后在交点相遇
1 | /** |
142. Linked List Cycle II
fast指针每次走两步, slow指针每次走一步
当二者相遇,说明存在环; 把slow放回head, 然后两个指针每次都走一步, 相遇点即为环的入口。
1 | /** |
148. Sort List
1 |