Linked List Cycle Ii

To solve this coding challenge, you need to detect the node where a cycle begins in a linked list. This problem requires an understanding of linked lists and specifically the concept of a cycle within them. You need to be able to identify if a cycle exists and where it starts using a two-pointer technique, often referred to as Floyd's Tortoise and Hare algorithm.

Step-by-Step Explanation

  1. Initialize Fast and Slow Pointers:
    • Two pointers are initialized at the head of the linked list. The
      fast
      pointer moves twice as fast as the
      slow
      pointer. This helps in detecting the cycle due to their different speeds.
  2. Find Intersection if Cycle Exists:
    • Move the
      slow
      pointer one step and the
      fast
      pointer two steps at a time. If there is a cycle, they will eventually meet at the same node. If the pointers meet, it confirms the presence of a cycle.
  3. Identify the Start of the Cycle:
    • To locate the starting node of the cycle, initialize another pointer at the head of the list. Then, move both this new pointer and the
      slow
      pointer one step at a time. The node where they meet will be the start of the cycle.
  4. Handle No Cycle Scenario:
    • If the
      fast
      pointer reaches the end of the list (i.e., it becomes
      null
      ), then there is no cycle in the linked list.

    Detailed Steps in Pseudocode

  5. Start by setting the
    slow
    and
    fast
    pointers at the head of the linked list.
  6. Traverse the list:
    • Move
      slow
      by one step.
    • Move
      fast
      by two steps.
    • Check if
      slow
      equals
      fast
      .
  7. If
    slow
    equals
    fast
    , there is a cycle:
    • Initialize a new pointer
      cycleStart
      at the head of the list.
    • Move both
      cycleStart
      and
      slow
      one step at a time.
    • When they meet, return that node as it is the starting point of the cycle.
  8. If
    fast
    reaches
    null
    or
    fast.next
    is
    null
    , return
    null
    as there is no cycle.
                                            
// Initialize two pointers at the head of the linked list
slowPointer = head
fastPointer = head

// Traverse the linked list to detect cycle
while fastPointer is not null and fastPointer.next is not null:
    // Move slow pointer one step
    slowPointer = slowPointer.next
    // Move fast pointer two steps
    fastPointer = fastPointer.next.next
    
    // Check if both pointers meet
    if slowPointer == fastPointer:
        // Cycle detected, find the start of the cycle
        cycleStart = head
        // Move both pointers one step at a time until they meet
        while cycleStart != slowPointer:
            cycleStart = cycleStart.next
            slowPointer = slowPointer.next
        // Return the start of the cycle
        return cycleStart

// If no cycle is found, return null
return null

                                        

Explanation

  • Initialization : Both
    slowPointer
    and
    fastPointer
    are set to the head of the list. This ensures that both start traversing the list from the same point.
  • Cycle Detection : The
    while
    loop continues until either the
    fastPointer
    reaches the end of the list (
    fastPointer
    or
    fastPointer.next
    becomes
    null
    ), or the two pointers meet. By moving
    slowPointer
    by one step and
    fastPointer
    by two steps, if there's a cycle, they will inevitably meet due to the differing speeds.
  • Locating the Cycle Start : Once a meeting point is confirmed, initialize another pointer (
    cycleStart
    ) at the head. Move both
    cycleStart
    and
    slowPointer
    one step at a time. The point at which they meet is the start of the cycle.
  • No Cycle : If
    fastPointer
    reaches
    null
    , it means there is no cycle, and we return
    null
    .
This method ensures that we detect a cycle and find the starting node of the cycle using constant space, as required.