Interview Bootcamp
0% completed
Solution: Start of LinkedList Cycle
Problem Statement
Given the head of a Singly LinkedList that contains a cycle, write a function to find the starting node of the cycle.
Solution
If we know the length of the LinkedList cycle, we can find the start of the cycle through the following steps:
- Take two pointers. Let’s call them
pointer1andpointer2. - Initialize both pointers to point to the start of the LinkedList.
- We can find the length of the LinkedList cycle using the approach discussed in LinkedList Cycle. Let’s assume that the length of the cycle is ‘K’ nodes.
- Move ``pointer2`
.....
.....
.....
Like the course? Get enrolled and start learning!
S
sharmilasiram
· 2 years ago
#class Node: # def __init__(self, value, next=None): # self.val = value # self.next = next class Solution: def findCycleStart(self, head): #TODO Write your code here if head == None or head.next == None: return None slow, fast = head, head while fast != None and fast.next != None: slow = slow.next fast = fast.next.next if slow == fast: slow = head while slow != fast: slow = slow.next fast = fast.next return slow return None
I feel like if you don't refine this code, this course is not even worth the money I am paying
Show 1 reply