GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/linkedlist/detectCycleii.py at master · deba999/leetcode Expand file tree/ Copy pathdetectCycleii.pyMore file actions 30 lines (27 loc) · 881 Bytes / Copy pathdetectCycleii.pyFile metadata and controls30 lines (27 loc) · 881 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """ slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: break else: return None while head != slow: head = head.next slow = slow.next return head # 大致思路 # 这道题不是那种一看就能知道做法的 # 大致意思是当Fast和Slow重合的时候,他们离开起始Loop的距离 # 和Head离开Loop起始的距离是相等的 # 所以有了以上代码的形式。 # 另外一种解法 # 可以用一个Set来储存遍历过得Node,然后若发现有重复 # 返回那个重复的点。 不过这个需要 O(N)的空间 # 违背了题目的要求。 |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |