forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list-cycle.py
More file actions
41 lines (38 loc) · 1.07 KB
/
linked-list-cycle.py
File metadata and controls
41 lines (38 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Two Pointers
#If the linked list has cycle, the fast pointer will eventually catch up.
#time: O(N).
#space: O(1).
class Solution(object):
def hasCycle(self, head):
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast is slow: return True
return False
#Flag
#Use a flag that stores on the nodes to know if we visited or not.
#time: O(N).
#space: O(N), for each node we use O(1) and there are N nodes.
class Solution(object):
def hasCycle(self, head):
curr = head
while curr:
if hasattr(curr, 'visited') and curr.visited: return True
curr.visited = True
curr = curr.next
return False
#HashSet
#Use a hash-set to store the visited nodes
#time: O(N).
#space: O(N).
class Solution(object):
def hasCycle(self, head):
visited = set()
curr = head
while curr:
if curr in visited: return True
visited.add(curr)
curr = curr.next
return False