代码随想录第四天

第一题 https://leetcode.cn/problems/swap-nodes-in-pairs/

python 复制代码
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        cur = dummy

        while cur.next and cur.next.next:
            temp1 = cur.next
            temp2 = cur.next.next.next

            cur.next = cur.next.next
            cur.next.next = temp1
            temp1.next = temp2
            cur = cur.next.next
        return dummy.next

画图后会清晰好多,指针怎么指就好写了

第二题 https://leetcode.cn/problems/remove-nth-node-from-end-of-list/ 这个属于比较简单的,善用虚拟头结点和双指针

python 复制代码
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy = ListNode(0, head)
        first = dummy
        second = dummy

        for i in range(n):
            first = first.next

        while first.next:
            first = first.next
            second = second.next
        second.next = second.next.next
        return dummy.next

第三题 https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/ 也比较简单

python 复制代码
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        cur1 = headA
        cur2 = headB

        while cur1 != cur2:
            cur1 = cur1.next if cur1 else headB
            cur2 = cur2.next if cur2 else headA
        return cur1

第四题 https://leetcode.cn/problems/linked-list-cycle-ii/ 难在理解怎么判断环和找到环的入口,这其实是算法上很经典的一个问题,大名鼎鼎的Floyd判圈算法,能自己想出来的人恭喜你,早出生几十年这个算法可能就是以你的名字命名了,我的水平理解这个算法都画了好久。。。。

1. 检测环的存在(龟兔赛跑)

  • 兔子(快指针)每次走两步

  • 乌龟(慢指针)每次走一步

  • 如果有环,兔子一定会追上乌龟(因为肯定要进入环里面,相对速度为1,这样走一圈就相当于相遇了)

  • 如果没环,兔子会先到达终点(None)

2. 找到环的入口(数学推导)

当兔子和乌龟第一次相遇后:

  • 把乌龟放回起点

  • 兔子和乌龟都改为每次走一步

  • 它们再次相遇的点就是环的入口

数学证明(为什么能找入口):

设:

  • 链表头到环入口距离:a

  • 环入口到第一次相遇点距离:b

  • 第一次相遇点到环入口距离:c

  • 环周长:L = b + c

第一次相遇时:

  • 乌龟走的路程:a + b

  • 兔子走的路程:a + b + n*L(兔子可能已经在环里转了n圈)

  • 因为兔子速度是乌龟的2倍:2(a + b) = a + b + n*L

  • 化简得:a + b = n*La = n*L - b = (n-1)*L + c

结论 :从链表头走 a 步 = 从相遇点走 c 步 + (n-1)

所以第二次相遇时,两者正好在环入口相遇!

python 复制代码
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        fast = slow = head

        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next

            if fast == slow:
                slow = head
                while slow != fast:
                    slow = slow.next
                    fast = fast.next
                return slow
            
        return None
相关推荐
琢磨先生David6 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
qq_454245036 天前
基于组件与行为的树状节点系统
数据结构·c#
超级大福宝6 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
岛雨QA6 天前
常用十种算法「Java数据结构与算法学习笔记13」
数据结构·算法
weiabc6 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
wefg16 天前
【算法】单调栈和单调队列
数据结构·算法
岛雨QA6 天前
图「Java数据结构与算法学习笔记12」
数据结构·算法
czxyvX6 天前
020-C++之unordered容器
数据结构·c++
岛雨QA6 天前
多路查找树「Java数据结构与算法学习笔记11」
数据结构·算法
AKA__Zas6 天前
初识基本排序
java·数据结构·学习方法·排序