-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_142.py
More file actions
33 lines (32 loc) · 850 Bytes
/
LeetCode_142.py
File metadata and controls
33 lines (32 loc) · 850 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
31
32
33
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow, fast = head, head
flag = False # 默认没有还
if not fast:
return None
if not fast.next:
return None
# 先判断有没有环
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
flag = True
break
if flag:
p = head
while p != fast:
fast = fast.next
p = p.next
return p
else:
return None