-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_cycle.py
52 lines (45 loc) · 1.16 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
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_end(self, data):
new_node = ListNode(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
return new_node
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
current = head
if head == None:
return False
setlist = set()
while current.next:
if current in setlist:
return True
else:
setlist.add(current)
return False
def main():
llist = LinkedList()
llist.insert_at_end(1)
llist.insert_at_end(2)
# llist.insert_at_end(0)
# llist.insert_at_end(-4)
llist.head
sol = Solution()
print(sol.hasCycle(llist.head))
if __name__ == "__main__":
main()