【leetcode】Linked List Cycle (python)

题目分析见这里

class Solution:
    # @param head, a ListNode
    # @return a list node
    def detectCycle(self, head):
        if None == head or None == head.next:
            return None
        pfast = head
        pslow = head
        #找第一次相遇的点,若存在环路,则肯定会相遇
        while pfast and pfast.next:
            pfast = pfast.next.next
            pslow = pslow.next
            if pslow == pfast: break
        if pslow != pfast:
            return None
        #pfast从头开始,则下次相遇的点就是循环开始的点
        pfast = head
        while True:
            if pfast == pslow:
                return pfast
            pfast = pfast.next
            pslow = pslow.next

【leetcode】Linked List Cycle (python)

时间: 2024-12-11 15:47:04

【leetcode】Linked List Cycle (python)的相关文章

【LeetCode】Linked List Cycle

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 做完Linked List Cycle II在做这题简直就是阉割版.. fast每次前进两步,slow每次前进一步,如果相遇则为true,否则false class Solution { public: bool hasCycle(ListNo

【Leetcode】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 思路:由[Leetcode]Linked List Cycle可知,利用一快一慢两个指针能够判断出链表是否存在环路.假设两个指针相遇之前slow走了s步,则fast走了2s步,并且fast已经在长度

【leetcode】Linked List Cycle II (middle)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

【LeetCode】【C++】Linked list cycle 2

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 此题不难,但想不用extra space做出来还是要动点脑筋的,因为我之前看过类似的算法题,所以就很快想到了. 方法是利用两个指针从头开始,指针p1一次走一步,指针p2一次走两步,如果有环则两指针必

LeetCode【141】Linked List Cycle

Given a linked list, determine if it has a cycle in it. 快慢步,直接AC代码: 值得注意一下的地方就是if(!p2->next || !p2->next->next),如果p2==NULL,那么p2->next用法是错误的,而||运算符的性质是当前一个条件为真时,第二个条件不再判断. bool hasCycle(ListNode *head) { if(!head || !head->next) return false

【leetcode82】Linked List Cycle

题目描述: 判断有序list是不是环 要求: 时间复杂度o(n) 原文描述: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 思路: 设置两个指针,一个快指针,每次走两步,一个慢指针,每次走一步 如果块指针==慢指针,那么包含环 注意判断空值和长度为一的情况 - 代码: /** * Definition for singly

【链表】Linked List Cycle

题目: Given a linked list, determine if it has a cycle in it. 思路: 对于判断链表是否有环,方法很简单,用两个指针,一开始都指向头结点,一个是快指针,一次走两步,一个是慢指针,一次只走一步,当两个指针重合时表示存在环了. fast先进入环,在slow进入之后,如果把slow看作在前面,fast在后面每次循环都向slow靠近1,所以一定会相遇,而不会出现fast直接跳过slow的情况. /** * Definition for singly

【Leetcode】Linked List Random Node

题目链接:https://leetcode.com/problems/linked-list-random-node/ 题目: Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely l