-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedListCycleII.py
More file actions
35 lines (32 loc) · 942 Bytes
/
linkedListCycleII.py
File metadata and controls
35 lines (32 loc) · 942 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/linked-list-cycle-ii/
# Author: Miao Zhang
# Date: 2021-01-21
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
'''
non-cycle: a
the perimeter of cycle: b
from the start of cycle to meeting point: c
slow: a + n * b + c
fast: 2(a + n * b + c)
fast - slow = n1 * b
a + c = n2 * b
'''
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
fast = head
while fast != slow:
fast = fast.next
slow = slow.next
return slow
return None