File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ """
10+ Intuition:
11+ ๋
ธ๋๋ง๋ค ๊ณ ์ ํ id๋ฅผ ์ ์ฅํ๊ณ ์ค๋ณต๋๋ id๊ฐ ์๋ค๋ฉด
12+ True๋ฅผ ๋ฐํํ๋ค. ๊ทธ ์ธ์๋ False์ด๋ค.
13+
14+ Time Complexity:
15+ O(N):
16+ ๊ฐ ๋
ธ๋๋ฅผ ํ๋ฒ์ฉ ์ค์บํ๋ฏ๋ก O(N)์ด ์์๋๋ค.
17+
18+ Space Complexity:
19+ O(N):
20+ ๊ฐ ๋
ธ๋์ id๋ฅผ ์ ์ฅํ๋ฏ๋ก O(N)์ด ์์๋๋ค.
21+ """
22+ node_ids = []
23+ node = head
24+ answer = False
25+ while node :
26+ node_id = id (node )
27+ if node_id not in node_ids :
28+ node_ids .append (node_id )
29+ else :
30+ answer = True
31+ break
32+ node = node .next
33+
34+ return answer
You canโt perform that action at this time.
0 commit comments