-
Notifications
You must be signed in to change notification settings - Fork 0
/
141.py
43 lines (37 loc) · 923 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
37
38
39
40
41
42
43
"""
Leetcode p141, do not have golang environment
"""
# Given a linked list, determine if it has a cycle in it.
# Follow up:
# Can you solve it without using extra space?
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class ListNode(object):
"""
Leetcode ListNode class
"""
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
"""
Leetcode sulution
"""
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is None:
return False
slow = head
quick = head.next
while quick != None and quick != slow:
slow = slow.next
quick = quick.next
if quick != None:
quick = quick.next
return quick != None