Skip to content

Commit 587f070

Browse files
committed
so simple
1 parent 8086933 commit 587f070

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

linkedListCycle.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Definition for singly-linked list.
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
# @param head, a ListNode
8+
# @return a boolean
9+
def hasCycle(head):
10+
fast = head
11+
slow = head
12+
while fast != None and fast.next != None and slow != None:
13+
fast = fast.next.next
14+
slow = slow.next
15+
if fast == slow:
16+
return True
17+
return False
18+
19+
a = ListNode('a')
20+
b = ListNode('b')
21+
c = ListNode('c')
22+
d = ListNode('d')
23+
a.next = b
24+
b.next = c
25+
c.next = d
26+
d.next = b
27+
assert hasCycle(a) == True
28+
29+
e = ListNode('e')
30+
f = ListNode('f')
31+
e.next = f
32+
assert hasCycle(e) == False

0 commit comments

Comments
 (0)