跳至内容
链表、二叉树与回溯

链表、二叉树与回溯

链表、二叉树与回溯

灵神题单:分享丨【算法题单】链表、二叉树与回溯(前后指针/快慢指针/DFS/BFS/直径/LCA/一般树) - 讨论 - 力扣(LeetCode)

场景:链表,二叉树

链表

回文链表

234. 回文链表 - 力扣(LeetCode)

  • 找中间结点模板、反转链表模板

  •  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    # 876. 链表的中间结点
        def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
            slow = fast = head
            while fast and fast.next:
                slow = slow.next
                fast = fast.next.next
            return slow
    
    # 206. 反转链表
        def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
            pre, cur = None, head
            while cur:
                nxt = cur.next
                cur.next = pre
                pre = cur
                cur = nxt
            return pre
    # 作者:灵神

旋转链表

61. 旋转链表 - 力扣(LeetCode)

  • 特殊情况、临界情况分析
最后更新于