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 1937d70 commit cc9a4d6Copy full SHA for cc9a4d6
1 file changed
linked-list-cycle/HYUNAHKO.py
@@ -0,0 +1,36 @@
1
+# Definition for singly-linked list.
2
+# class ListNode:
3
+# def __init__(self, x):
4
+# self.val = x
5
+# self.next = None
6
+
7
+class Solution:
8
+ def hasCycle(self, head: Optional[ListNode]) -> bool:
9
+ visited = set()
10
11
+ current = head
12
+ while current:
13
+ if current in visited:
14
+ return True
15
16
+ visited.add(current)
17
+ current = current.next
18
19
+ return False
20
21
+# w/o set()
22
23
24
+ if not head or not head.next:
25
26
27
+ slow = head
28
+ fast = head.next
29
30
+ while slow != fast:
31
+ if not fast or not fast.next:
32
33
+ slow = slow.next
34
+ fast = fast.next.next
35
36
0 commit comments