-
Notifications
You must be signed in to change notification settings - Fork 0
/
0141_linkedlist.py
64 lines (45 loc) · 1.52 KB
/
0141_linkedlist.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
# https://leetcode.com/problems/linked-list-cycle/
# The description of input format is so confusing...
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head):
fast = head
slow = head
while (fast is not None) and (fast.next is not None):
fast = fast.next.next
#if slow is not None:
slow = slow.next
if fast == slow:
return True
return False
"""
Also 0142: Linked List Cycle II
It's more like a math problem, learnt from the LeetCode book
"""
# Runtime: 32 ms, faster than 96.73% of Python online submissions for Linked List Cycle II.
# Memory Usage: 19.9 MB, less than 21.69% of Python online submissions for Linked List Cycle II.
class Solution2(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
fast = head
slow = head
while (fast is not None) and (fast.next is not None):
fast = fast.next.next
slow = slow.next
if fast == slow:
break
# no cycle
if fast is None or (fast is not None and fast.next is None):
return None
fast = head
while (fast != slow):
fast = fast.next
slow = slow.next
return fast