
160. 相交链表 - 力扣LeetCode# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val x # self.next None class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) - Optional[ListNode]: a,b headA,headB while a is not b: a a.next if a else headB b b.next if b else headA return a本质交换跑道消除长度差易错点及注意事项① 为什么不相交也能正常结束各走 mn 步后同时变成 Nonea is not b不成立② 比较链表节点时必须用is 比较节点对象不能比 val206. 反转链表 - 力扣LeetCode# Definition for singly-linked list. # class ListNode: # def __init__(self, val0, nextNone): # self.val val # self.next next class Solution: def reverseList(self, head: ListNode | None) - ListNode | None: prev,cur None,head while cur: // 保存下一个节点 nxt cur.next // 反转当前节点指针 cur.next prev // 前移 prev 指针 prev cur // 前移 cur 指针 cur nxt return prev易错点及注意事项① 最终返回结果为prev而非head因为反转后此时 head 已是某个尾节点② 四行循环体顺序不能乱先存 nxt 是第一步234. 回文链表 - 力扣LeetCodeclass Solution: def isPalindrome(self, head: Optional[ListNode]) - bool: # 1. 快慢指针找中点偶数长度时 slow 停在中间偏右 slow fast head while fast and fast.next: slow slow.next fast fast.next.next # 2. 反转后半段从 slow 开始 prev, cur None, slow while cur: nxt cur.next cur.next prev prev cur cur nxt # 3. 从两端向中间比对以后半段较短或等长为准 first, second head, prev while second: if first.val ! second.val: return False first first.next second second.next return True口诀找中点 反转 双指针比对易错点及注意事项① 第三步 “比对” 时循环条件用while second反转出的后半段一定不长于前半段走完即可判定结束② 追问 要求不修改链表应当如何处理比对完再将后半段反转回去接上141. 环形链表 - 力扣LeetCode# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val x # self.next None class Solution: def hasCycle(self, head: Optional[ListNode]) - bool: slowfasthead while fast and fast.next: slow slow.next fast fast.next.next if slow is fast: return True return False142. 环形链表 II - 力扣LeetCode# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val x # self.next None class Solution: def detectCycle(self, head: Optional[ListNode]) - Optional[ListNode]: slowfasthead while fast and fast.next: slow slow.next fast fast.next.next if slow is fast: phead while p is not slow: p p.next slow slow.next return p return None21. 合并两个有序链表 - 力扣LeetCode# Definition for singly-linked list. # class ListNode: # def __init__(self, val0, nextNone): # self.val val # self.next next class Solution: def mergeTwoLists(self, list1: ListNode | None, list2: ListNode | None) - ListNode | None: dummyListNode(0) curdummy # 注意这个循环条件 while list1 and list2: if list1.vallist2.val: cur.nextlist1 list1list1.next else: cur.nextlist2 list2list2.next cur cur.next cur.next list1 or list2 return dummy.next为啥最后返回 dummy.next而不是 cur.next因为dummy一直固定在链表最前面而cur会不断向后移动。146. LRU 缓存 - 力扣LeetCode树相关题目102. 二叉树的层序遍历 - 力扣LeetCodefrom collections import deque # Definition for a binary tree node. # class TreeNode: # def __init__(self, val0, leftNone, rightNone): # self.val val # self.left left # self.right right class Solution: def levelOrder(self, root: Optional[TreeNode]) - List[List[int]]: if not root: return [] res [] q deque([root]) while q: size len(q) level [] for i in range(size): node q.popleft() level.append(node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) res.append(level) return res