-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path141.py
45 lines (33 loc) · 978 Bytes
/
141.py
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
34
35
36
37
38
39
40
41
42
43
44
45
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @return a boolean
# original solution
def hasCycle(self, head):
if head == None:
return False
head.val = 'A'
p = head.next
while p != None:
if p.val == 'A':
return True
else:
p.val = 'A'
p = p.next
return False
# new solution fast and slow runners
def hasCycle(self, head):
if head == None:
return False
fast = head
slow = head
while fast != None and fast.next != None:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False