博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】Linked List Cycle (python)
阅读量:4067 次
发布时间:2019-05-25

本文共 621 字,大约阅读时间需要 2 分钟。

题目分析见

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

转载地址:http://pumji.baihongyu.com/

你可能感兴趣的文章
STM32中assert_param的使用
查看>>
C语言中的 (void*)0 与 (void)0
查看>>
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
23. Merge k Sorted Lists (Divide and conquer, Linked List) 以及java匿名内部类
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>