LeetCode刷题指南
第 0 章 hot100
0.1 哈希
0.2 双指针
0.3 滑动窗口
0.4 子串
0.5 普通数组
0.6 矩阵
0.7 链表
0.8 二叉树
0.9 图论
0.10 回溯
0.11 二分查找
0.12 栈
0.13 堆
0.14 贪心算法
0.15 动态规划
0.16 多维动态规划
0.17 技巧
第0-1章 面试经典150
0.1 数组/字符串
0.2 双指针
0.3 滑动窗口
链表
二叉树
第 1 章 最易懂的贪心算法
1.1 算法解释
1.2 分配问题
1.3 区间问题
1.4 练习
第 2 章 玩转双指针
2.1 算法解释
2.2 Two Sum
2.3 归并两个有序数组
2.4 滑动窗口
2.5 快慢指针
2.6 练习
第 3 章 居合斩!二分查找
3.1 算法解释
3.2 求开方
3.3 查找区间
3.4 查找峰值
3.5 旋转数组查找数字
3.6 练习
第 4 章 千奇百怪的排序算法
4.1 常用排序算法
4.2 快速选择
4.3 桶排序
4.4 练习
第 5 章 一切皆可搜索
5.1 算法解释
5.2 深度优先搜索
5.3 回溯法
5.4 广度优先搜索
5.5 练习
第 6 章 深入浅出动态规划
6.1 算法解释
6.2 基本动态规划:一维
6.3 基本动态规划:二维
6.4 分割类型题
6.5 子序列问题
6.6 背包问题
6.7 字符串编辑
6.8 股票交易
6.9 练习
第 7 章 化繁为简的分治法
7.1 算法解释
7.2 表达式问题
7.3 练习
第 8 章 巧解数学问题
8.1 引言
8.2 公倍数与公因数
8.3 质数
8.4 数字处理
8.5 随机与取样
8.6 练习
第 9 章 神奇的位运算
9.1 常用技巧
9.2 位运算基础问题
9.3 二进制特性
9.4 练习
第 10 章 妙用数据结构
10.1 C++ STL
10.2 Python 常用数据结构
10.3 数组
10.4 栈和队列
10.5 单调栈
10.6 优先队列
10.7 双端队列
10.8 哈希表
10.9 多重集合和映射
10.10 前缀和与积分图
10.11 练习
第 11 章 令人头大的字符串
11.1 引言
11.2 字符串比较
11.3 字符串理解
11.4 字符串匹配
11.5 练习
第 12 章 指针三剑客之一:链表
12.1 数据结构介绍
12.2 链表的基本操作
12.3 其它链表技巧
12.4 练习
第 13 章 指针三剑客之二:树
13.1 数据结构介绍
13.2 树的递归
13.3 层次遍历
13.4 前中后序遍历
13.5 二叉查找树
13.6 字典树
13.7 练习
第 14 章 指针三剑客之三:图
14.1 数据结构介绍
14.2 二分图
14.3 拓扑排序
14.4 练习
第 15 章 更加复杂的数据结构
15.1 引言
15.2 并查集
15.3 复合数据结构
15.4 练习
第16章 面试题
第 17 章 十大经典排序算法
README
本文档使用 MrDoc 发布
-
+
首页
0.7 链表
[160. 相交链表](https://leetcode.cn/problems/intersection-of-two-linked-lists/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def getIntersectionNode( self, headA: ListNode, headB: ListNode ) -> Optional[ListNode]: node1 = headA node2 = headB while node1 != node2: node1 = node1.next if node1 else headB node2 = node2.next if node2 else headA if node1 == node2: return node1 return False ``` [206. 反转链表](https://leetcode.cn/problems/reverse-linked-list/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: pre = None cur = head while cur: nxt = cur.next cur.next = pre pre = cur cur = nxt return pre ``` [234. 回文链表](https://leetcode.cn/problems/palindrome-linked-list/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: if not head or not head.next: return True slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next pre = None cur = slow while cur: nxt = cur.next cur.next = pre pre = cur cur = nxt left, right = head, pre while right: if left.val != right.val: return False left = left.next right = right.next return True ``` [141. 环形链表](https://leetcode.cn/problems/linked-list-cycle/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: return True return False ``` [142. 环形链表 II](https://leetcode.cn/problems/linked-list-cycle-ii/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: fast = head while slow != fast: slow = slow.next fast = fast.next return slow return None # 开始与环距离a # 环口到第一次相遇b # 第一次相遇到还口c # fast:a + b + n(b+c) # slow: a + b # a + b + n(b+c) = 2(a+b) -> a = c + (n-1)(b+c) ``` [21. 合并两个有序链表](https://leetcode.cn/problems/merge-two-sorted-lists/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: if not list1:return list2 if not list2:return list1 if list1.val < list2.val: list1.next = self.mergeTwoLists(list1.next, list2) return list1 else: list2.next = self.mergeTwoLists(list1, list2.next) return list2 ``` [*2. 两数相加](https://leetcode.cn/problems/add-two-numbers/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def addTwoNumbers( self, l1: Optional[ListNode], l2: Optional[ListNode] ) -> Optional[ListNode]: dummy = node = ListNode() remain = 0 while l2 or l1: l1_val = l1.val if l1 else 0 l2_val = l2.val if l2 else 0 total = l1_val + l2_val + remain remain = total // 10 node.next = ListNode(total % 10) if l1: l1 = l1.next if l2: l2 = l2.next node = node.next if remain != 0: node.next = ListNode(remain) return dummy.next ``` [19. 删除链表的倒数第 N 个结点](https://leetcode.cn/problems/remove-nth-node-from-end-of-list/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: dummy = ListNode() dummy.next = head slow = fast = dummy for _ in range(n): fast = fast.next while fast and fast.next: slow = slow.next fast = fast.next slow.next = slow.next.next return dummy.next ``` [24. 两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: node = head for _ in range(2): if not node: return head node = node.next pre = None cur = head for _ in range(2): nxt = cur.next cur.next = pre pre = cur cur = nxt head.next = self.swapPairs(cur) return pre ``` [25. K 个一组翻转链表](https://leetcode.cn/problems/reverse-nodes-in-k-group/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: # 递归终止条件:剩余节点不足K个 node = head for _ in range(k): if not node: return head node = node.next # 翻转当前K个节点 pre = None cur = head for _ in range(k): nxt = cur.next cur.next = pre pre = cur cur = nxt # 连接后续递归结果 head.next = self.reverseKGroup(cur, k) return pre ``` [138. 随机链表的复制](https://leetcode.cn/problems/copy-list-with-random-pointer/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]': if not head: return None dic = {} node = head while node: dic[node] = Node(node.val) node = node.next node = head while node: dic[node].next = dic.get(node.next) dic[node].random = dic.get(node.random) node = node.next return dic[head] ``` [148. 排序链表](https://leetcode.cn/problems/sort-list/?envType=study-plan-v2&envId=top-100-liked) ```py class Solution: def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head or not head.next: return head slow, fast = head, head.next while fast and fast.next: slow = slow.next fast = fast.next.next mid = slow.next slow.next = None l1 = self.sortList(head) l2 = self.sortList(mid) return self.merge(l1, l2) def merge(self, l1, l2): if not l2: return l1 if not l1: return l2 if l1.val < l2.val: l1.next = self.merge(l1.next, l2) return l1 else: l2.next = self.merge(l1, l2.next) return l2 ``` [23. 合并 K 个升序链表]() ```python class Solution: def mergeTwoList(self, l1, l2): if not l1: return l2 if not l2: return l1 if l1.val < l2.val: l1.next= self.mergeTwoList(l1.next, l2) return l1 else: l2.next= self.mergeTwoList(l1, l2.next) return l2 def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: n = len(lists) if n == 0: return None if n == 1: return lists[0] mid = n // 2 left_half = self.mergeKLists(lists[:mid]) right_half = self.mergeKLists(lists[mid:]) return self.mergeTwoList(left_half,right_half) ``` ```python class Solution: def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: cur = dummy = ListNode() if len(lists) == 0: return None h = [(head.val, i) for i, head in enumerate(lists) if head] heapq.heapify(h) while h: v,i = heappop(h) cur.next = ListNode(v) cur = cur.next if lists[i].next: lists[i] = lists[i].next heapq.heappush(h, (lists[i].val,i)) return dummy.next ``` [146. LRU 缓存]() ```python class LRUCache: def __init__(self, capacity: int): self.cache = OrderedDict() self.capacity = capacity def get(self, key: int) -> int: if key not in self.cache: return -1 self.cache.move_to_end(key) return self.cache[key] def put(self, key: int, value: int) -> None: if key in self.cache: self.cache.move_to_end(key) self.cache[key] = value if len(self.cache) > self.capacity: self.cache.popitem(last=False) ```
嘉心糖糖
2025年9月6日 19:20
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码