-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path141.py
36 lines (33 loc) · 890 Bytes
/
141.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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle2(self, head):
"""
:type head: ListNode
:rtype: bool
"""
nodelist = set()
currentnode = head
while currentnode is not None:
if currentnode not in nodelist:
nodelist.add(currentnode)
currentnode = currentnode.next
else:
return True
return False
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head:
return False
p1, p2 = head, head
while(p2.next and p2):
p1, p2 = p1.next, p2.next.next
if p1 == p2:
return True
return False