Skip to content

Commit 5e5a8e7

Browse files
committed
#225 linked-list-cycle solution
1 parent 1fdda6c commit 5e5a8e7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

linked-list-cycle/sungjinwi.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)