-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinked-list-cycle-ii.py
More file actions
42 lines (38 loc) · 1.9 KB
/
linked-list-cycle-ii.py
File metadata and controls
42 lines (38 loc) · 1.9 KB
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
# https://leetcode.com/problems/linked-list-cycle-ii/
# Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
#
# Note: Do not modify the linked list.
#
# 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
# this solution builds on the previos problem of finding a cycle in a linked list .
# As I already mentioned , I still dont completely understand the intuition behind this, in fact I now need to understand another extension
class Solution(object):
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
# Step 1 : Initiate two pointers slow and fast and point them to the head of the linked list
fast,slow = head , head
while fast and fast.next:
# Step 2 : Increment fast pointer by 2 , slow by 1
fast,slow = fast.next.next , slow.next
# Step 3 : for every increment of the two pointers , check if the fast & slow pointers now point to the same node.
if fast is slow:
# Step 4 : Finally when they meet, it concludes the presence of a loop,but unlike the previous problem - its not sufficient to have just detected the loop
# we also need to find where the loop begins.
# Step 5 : Once we detect the existence of a loop , we reset the slow pointer back to the head
slow = head
while fast is not slow:
# Step 6 : We now move both slow and fast pointer by 1 till they meet again
fast,slow = fast.next ,slow.next
# Step 7 : The point they meet gives the 'beginning of the cycle'
return fast # Could return fast or slow pointer since they point to the same here.
return None