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 1fdda6c commit 5e5a8e7Copy full SHA for 5e5a8e7
1 file changed
linked-list-cycle/sungjinwi.py
@@ -0,0 +1,29 @@
1
+"""
2
+ 풀이 :
3
+ set에 지나온 node를 저장하고 새 node로 이동하면 set안에 존재하는지 확인
4
+ 이미 존재하는 node를 지나면 True
5
+ None에 도달하면 False
6
+
7
+ 노드의 길이 = n
8
9
+ TC : O(N)
10
11
+ SC : O(N)
12
13
14
+# Definition for singly-linked list.
15
+# class ListNode:
16
+# def __init__(self, x):
17
+# self.val = x
18
+# self.next = None
19
20
+class Solution:
21
+ def hasCycle(self, head: Optional[ListNode]) -> bool:
22
+ visited = set()
23
+ while head :
24
+ if head in visited :
25
+ return True
26
+ else :
27
+ visited.add(head)
28
+ head = head.next
29
+ return False
0 commit comments