-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasCycle.py
More file actions
88 lines (78 loc) · 1.91 KB
/
hasCycle.py
File metadata and controls
88 lines (78 loc) · 1.91 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
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
# Definition for singly-linked list.
import pdb
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
"""
hash
76/52 ms
17.5 MB
maintain an address (or uniqe ID) lookup table
"""
if not head:
return False
lookup = set()
pdb.set_trace()
while head:
if id(head) in lookup:
return True
lookup.add(id(head))
head = head.next
return False
def hasCycle_2p(self, head: ListNode) -> bool:
"""
52 ms
16.9 MB
two speed pointers, chasing problem
"""
fast, slow = head, head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
def hasCycle_setnone(self, head: ListNode) -> bool:
"""
hash
60 ms
16.9 MB
"""
if not head:
return False
while head.next and head.val != None:
head.val = None
head = head.next
if not head:
return False
return True
def genList(l: list) -> ListNode:
prenode = ListNode(0)
lastnode = prenode
for val in l:
lastnode.next = ListNode(val)
lastnode = lastnode.next
return prenode.next
def printList(l: ListNode):
outstr = ''
while l:
outstr = outstr + str(l.val)
l = l.next
if l:
outstr = outstr + ' -> '
print(outstr)
if __name__ == '__main__':
app = Solution()
num1 = '34'
num2 = '789012'
l1 = [int(c) for c in num1[::1]]
l2 = [int(c) for c in num2[::1]]
a = genList(l1)
b = genList(l2)
printList(a)
printList(b)
print(a.next.next)
print(app.hasCycle(b))