代码随想录第四天

第一题 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
相关推荐
CoderCodingNo2 小时前
【GESP】C++六级考试大纲知识点梳理, (3) 哈夫曼编码与格雷码
开发语言·数据结构·c++
牛马大师兄2 小时前
数据结构复习 | 什么是数据结构?
数据结构
wu_asia3 小时前
方阵对角线元素乘积计算
数据结构·算法
想逃离铁厂的老铁3 小时前
Day43 >> 300.最长递增子序列 + 674. 最长连续递增序列+ 718. 最长重复子数组
数据结构·算法
宵时待雨4 小时前
数据结构(初阶)笔记归纳4:单链表的实现
c语言·开发语言·数据结构·笔记·算法
wm10434 小时前
代码随想录第三天 链表
数据结构·链表
BLSxiaopanlaile4 小时前
关于子集和问题的几种解法
数据结构·算法·剪枝·回溯·分解
じ☆冷颜〃5 小时前
交换代数的解析延拓及在CS的应用
c语言·数据结构·笔记·线性代数·密码学
程序员-King.5 小时前
day136—快慢指针—重排链表(LeetCode-143)
算法·leetcode·链表·快慢指针