We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8086933 commit 587f070Copy full SHA for 587f070
linkedListCycle.py
@@ -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