-
Notifications
You must be signed in to change notification settings - Fork 43
/
linked-list-cycle.py
200 lines (170 loc) · 4.94 KB
/
linked-list-cycle.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
Given head, the head of a linked list,
determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list
that can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's next pointer is connected to.
Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
Constraints:
The number of the nodes in the list is in the range [0, 104].
-105 <= Node.val <= 105
pos is -1 or a valid index in the linked-list.
Follow up: Can you solve it using O(1) (i.e. constant) memory?
"""
# V0
# IDEA : fast-slow pointer (2 pointers)
# https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/2_pointers.md
class Solution(object):
def hasCycle(self, head):
if not head:
return False
fast = slow = head
"""
NOTE : below conditions (while fast and fast.next)
"""
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
# V0'
# IDEA : fast-slow pointer (2 pointers)
# https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/2_pointers.md
class Solution:
def hasCycle(self, head):
if not head:
return False
slow = fast = head
while fast != None and fast.next != None:
#NOTE : need to do move slow, fast pointer then compare them
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
# V0
# IDEA : MAP
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
map = {}
while head:
if id(head) in map:
return True
else:
map[id(head)] = True
head = head.next
return False
# V1
# https://blog.csdn.net/coder_orz/article/details/51516558
# IDEA : MAP
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
map = {}
while head:
if id(head) in map:
return True
else:
map[id(head)] = True
head = head.next
return False
# V1'
# https://blog.csdn.net/coder_orz/article/details/51516558
# IDEA : TWO POINTER
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
# V1''
# https://blog.csdn.net/coder_orz/article/details/51516558
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head and head.next and head == self.reverseList(head):
return True
return False
def reverseList(self, head):
before = after = None
while head:
after = head.next
head.next = before
before = head
head = after
return before
# V2
# Time: O(n)
# Space: O(1)
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
fast, slow = head, head
while fast and fast.__next__:
fast, slow = fast.next.__next__, slow.__next__
if fast == slow:
return True
return False
# V4
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
# @param head, a ListNode
# @return a boolean
def hasCycle(self, head):
fast, slow = head, head
while fast and fast.__next__:
fast, slow = fast.next.__next__, slow.__next__
if fast is slow:
return True
return False
# if __name__ == "__main__":
# head = ListNode(1)
# head.next = ListNode(2)
# head.next.next = ListNode(3)
# head.next.next.next = head.next
# print Solution().hasCycle(head)