Leetcode Linked List
160. Intersection of Two Linked Lists
Description
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
Click to view full description
Example 1:
- Input:
intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 - Output:
Intersected at '8'
Example 2:
- Input:
intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 - Output:
Intersected at '2'
Solution
Idea: 使用双指针,两个指针分别遍历两个链表。指针遍历完一个链表后,再遍历另一个链表,这样可以保证两个链表会 同时到达交点 或者 同时到达链表末尾(如果没有交点)。
Complexity: Time: O(m + n), Space: O(1)
1 | # Definition for singly-linked list. |
206. Reverse Linked List
Description
Given the head of a singly linked list, reverse the list, and return the reversed list.
Click to view full description
Example 1:
- Input:
head = [1,2,3,4,5] - Output:
[5,4,3,2,1]
Example 2:
- Input:
head = [1,2] - Output:
[2,1]
Solution
Idea: 使用迭代法,遍历链表,将每个节点的 next 指向前一个节点,最后返回 pre 即为最后一个节点。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
234. Palindrome Linked List
Description
Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
Click to view full description
Example 1:
- Input:
head = [1,2,2,1] - Output:
true
Example 2:
- Input:
head = [1,2] - Output:
false
Solution
Idea: 使用快慢指针找到链表的中点,然后反转后半部分链表(slow 指针之后的部分),最后比较前半部分和后半部分是否一致。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
141. Linked List Cycle
Description
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Click to view full description
Example 1:
- Input:
head = [3,2,0,-4], pos = 1 - Output:
true
Example 2:
- Input:
head = [1,2], pos = 0 - Output:
true
Solution
Idea: 使用快慢指针,如果快指针和慢指针相遇,则说明有环。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
142. Linked List Cycle II
Description
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
Click to view full description
Example 1:
- Input:
head = [3,2,0,-4], pos = 1 - Output:
2
Example 2:
- Input:
head = [1,2], pos = 0 - Output:
1
Solution
Idea: 使用快慢指针,如果快慢指针相遇,则说明有环。然后从相遇点开始,将 slow 指针移动到链表头部,然后将 slow 和 fast 指针每次移动一步,直到相遇,相遇点即为环的入口。
为什么 slow 指针从链表头部开始,会和 fast 指针在环的入口相遇?
x:从 head 到环的起始点的距离y:环的起始点到 slow 和 fast 第一次相遇点的距离z:从相遇点回到环的起始点的距离(即环的剩余部分)
假设 slow 和 fast 在相遇时,slow 走了 x + y 步,fast 走了 x + y + z + y 步。
因为 fast 的速度是 slow 的两倍,所以 x + y + z + y = 2(x + y),即 z = x。
所以,slow 从链表头部开始,走 x 步,就会到达环的入口。而 fast 从相遇点开始,走 z 步,也会到达环的入口。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
21. Merge Two Sorted Lists
Description
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Click to view full description
Example 1:
- Input:
list1 = [1,2,4], list2 = [1,3,4] - Output:
[1,1,2,3,4,4]
Example 2:
- Input:
list1 = [], list2 = [0] - Output:
[0]
Solution
Idea: 使用一个虚拟头节点来存储结果,然后遍历两个链表, 比较两个链表的值,将较小的值插入到虚拟头节点中,并移动指针。最后,将剩余的链表插入到虚拟头节点中。
Complexity: Time: O(m + n), Space: O(1)
1 | # Definition for singly-linked list. |
2. Add Two Numbers
Description
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Click to view full description
Example 1:
- Input:
l1 = [2,4,3], l2 = [5,6,4] - Output:
[7,0,8]
Solution
Idea: 使用一个虚拟头节点来存储结果,然后遍历两个链表,计算每一位的和还有进位,并存储到虚拟头节点中。
Complexity: Time: O(max(m, n)), Space: O(max(m, n))
1 | # Definition for singly-linked list. |
19. Remove Nth Node From End of List
Description
Given the head of a linked list, remove the nth node from the end of the list and return its head.
Click to view full description
Example 1:
- Input:
head = [1,2,3,4,5], n = 2 - Output:
[1,2,3,5]
Example 2:
- Input:
head = [1], n = 1 - Output:
[]
Solution
Idea: 要删除倒数第n个节点,我们需要知道它的前一个节点。可以先遍历一遍链表,找到链表的长度,然后从链表的头部开始,找到需要删除的节点的前一个节点,对需要删除的节点进行删除。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
24. Swap Nodes in Pairs
Description
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list’s nodes (i.e., only nodes themselves may be changed.)
Click to view full description
Example 1:
- Input:
head = [1,2,3,4] - Output:
[2,1,4,3]
Example 2:
- Input:
head = [1] - Output:
[1]
Solution
Idea: 使用一个虚拟头节点来存储结果,然后遍历链表,交换每两个相邻的节点。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
138. Copy List with Random Pointer
Description
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointers of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
Return the head of the copied linked list.
The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
val: an integer representingNode.valrandom_index: the index of the node (range from0ton-1) that the random pointer points to, ornullif it does not point to any node.
Your code will only be given the head of the original linked list.
Click to view full description
Example 1:
- Input:
head=[[7,null],[13,0],[11,4],[10,2],[1,0]] - Output:
[[7,null],[13,0],[11,4],[10,2],[1,0]]
Example 2:
- Input:
head = [[1,1],[2,1]] - Output:
[[1,1],[2,1]]
Solution
Idea: 第一步:复制每个节点,并插入到原节点之后(1->1'->2->2'->3->3')。第二步:设置新节点的 random 指针。第三步:将复制链表和原链表拆分出来。
Complexity: Time: O(n), Space: O(1)
1 | """ |
148. Sort List
Description
Given the head of a linked list, return the list after sorting it in ascending order.
Click to view full description
Example 1:
- Input:
head = [4,2,1,3] - Output:
[1,2,3,4]
Example 2:
- Input:
head = [-1,5,3,4,0] - Output:
[-1,0,3,4,5]
Solution
Approach 1 归并排序(递归)
Idea: 使用归并排序来对链表进行排序。
归并排序 是分治法的典型应用,它将链表分成两个部分,然后递归地对两个部分进行排序,最后将两个有序的链表合并成一个有序的链表。
具体步骤:
- 找到链表的中点,将链表分成两个部分。
- 递归地对两个部分进行排序。
- 合并两个有序的链表。
Complexity: Time: O(n log n), Space: O(log n)
1 | # Definition for singly-linked list. |
Approach 2 归并排序(迭代)
Idea: 使用迭代的方式来实现归并排序。
Complexity: Time: O(n log n), Space: O(1)
迭代的方法可以将空间复杂度缩短为 _O(1)_, 但代码逻辑会变得复杂不易理解。具体可见官方题解。
146. LRU Cache
Description
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(int capacity)Initialize the LRU cache with positive sizecapacity.int get(int key)Return the value of thekeyif thekeyexists, otherwise return-1.void put(int key, int value)Update the value of thekeyif thekeyexists. Otherwise, add thekey-valuepair to the cache. If the number of keys exceeds thecapacityfrom this operation, evict the least recently used key.
The functions get and put must each run in O(1) time complexity.
Click to view full description
Example 1:
- Input:
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] - Output:
[null, null, null, 1, null, -1, null, -1, 3, 4]
Solution
Idea: 使用双向链表和哈希表来实现 LRU 缓存。双向链表 用于维护节点的顺序,哈希表 用于存储节点和快速查找节点。
使用 双向链表 而非 单向链表 的原因是:在删除节点时,单向链表需要遍历链表找到前一个节点,时间复杂度为 O(n) 。而双向链表可以直接找到前一个节点,时间复杂度为 O(1) 。
get 操作:
- 如果
key存在,则将该节点移到链表尾部,并返回该节点的值。 - 如果
key不存在,则返回-1。
put 操作:
- 如果
key存在,则更新该节点的值,并将其移到链表尾部。 - 如果
key不存在,则插入该节点。如果插入后链表长度超过capacity,则删除链表头部的节点。
Complexity: Time: O(1), Space: O(capacity)
1 | class ListNode: |
Top Interview 150 补充
92. Reverse Linked List II
Description
Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Click to view full description
Example 1:
- Input:
head = [1,2,3,4,5], left = 2, right = 4 - Output:
[1,4,3,2,5]
Example 2:
- Input:
head = [5], left = 1, right = 1 - Output:
[5]
Solution
Idea: 使用一个虚拟头节点来存储结果,然后遍历链表,找到需要反转的节点区间,并进行反转。
具体反转步骤:
- 把
current.next指向next.next - 把
next.next指向pre.next - 把
pre.next指向next

Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
82. Remove Duplicates from Sorted List II
Description
Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Click to view full description
Example 1:
- Input:
head = [1,2,3,3,4,4,5] - Output:
[1,2,5]
Example 2:
- Input:
head = [1,1,1,2,3] - Output:
[2,3]
Solution
Idea: 遍历链表,如果 当前节点的下一个节点 和 下一个节点的下一个节点 的值相同,则删除当前节点之后的节点,直到找到下一个不重复的节点。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
61. Rotate List
Description
Given the head of a linked list, rotate the list to the right by k places.
Click to view full description
Example 1:
- Input:
head = [1,2,3,4,5], k = 2 - Output:
[4,5,1,2,3]
Example 2:
- Input:
head = [0,1,2], k = 4 - Output:
[2,0,1]
Solution
Idea: 这道题的输入 k 可能大于链表的长度,为了不超时,需要先计算链表的长度,然后取模得到真正需要旋转的次数。每次旋转时,找到 倒数第二个节点,将 最后一个节点 插入到 第一个节点 之前,并更新 head 为最新的头节点。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |
86. Partition List
Description
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each partition.
Click to view full description
Example 1:
- Input:
head = [1,4,3,2,5,2], x = 3 - Output:
[1,2,2,4,3,5]
Example 2:
- Input:
head = [2,1], x = 2 - Output:
[1,2]
Solution
Idea: 根据 x 的值,将链表分为两部分,一部分是小于 x 的链表 less,另一部分是大于等于 x 的链表 more。最后将两部分链表拼接起来即可。
Complexity: Time: O(n), Space: O(1)
1 | # Definition for singly-linked list. |


