Thursday, December 25, 2014

An algorithm for returning the node at the beginning of a loop in a singly linked list.

Q. Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.

Circular linked list: A (corrupt) linked list in which a node's next pointer points to an earlier node, so as to make a loop in the linked list.


Input: A -> B -> C -> D -> E -> C (the same C as earlier)

Output: C


#1. Use a Set to record every node that you visit. When a node that is already present in the list is encountered, the is the beginning of the loop. If pointer encounters null, no loop is present.

#2. Consider two pointers p1 and p2. p2 travels 2 nodes at a time, p1 travels 1 node at a time. Let them meet (if there is a loop, they will surely meet but if there isn't the fast node will encounter a null). Now that they have met, set p1 to head of the list. 

Now let p1 and p2 travel 1 node at a time. The point at which they meet again is the start of the loop. Now we just return that point.

Why does this happen?

Consider the case shown above. When P1 reaches the node at the head of the loop, it has hopped 4 times while P2 has hopped 8 times. Now if we put P1 back to the head of the linked list and make it go one node at a time, while we make P2 go reverse 1 node at a time they will meet at the start of the loop again (since P2 has travelled 2K-K = K more nodes, if P1 starts again from the head, and P2 rolls back, they meet at the start of loop after hopping 4 times.). The point where P1 and P2 meet is the point where this situation is reversed, now instead of P2 travelling back, it travels to the front to meet P1 at the start of the loop again if P1 had to begin travelling from the head of the linked list.


import java.util.HashSet;

public class Code{
    //Method 1
    public static <T> LinkedList.Node returnLoopStartNode(LinkedList<T> ll){ 
        HashSet<LinkedList.Node> record = new HashSet<>();
        LinkedList<T>.Node p = ll.head; 
             else return p;
             p =;
        //No cycle here.
        return null;
    //Method 2 (Preferred)
    public static <T> LinkedList.Node returnLoopStartNodeM2(LinkedList<T> ll){
        LinkedList.Node p1 = ll.head, p2 = ll.head;
        if(!=null &&!=null)p2 =;else return null;//No cycle
        p1 =;
            p1 =;
            if(!=null &&!=null) p2 =;
            else return null;//No cycle
        p1 = ll.head;
        while(p1!=p2){p1 =;p2 =;}
        return p1; 
    //Links end node to the specified index
    public static <T> LinkedList<T> makeCycleList(T[] array, int index){
        LinkedList<T> ll = new LinkedList<>();
        ll.head = Node();
        LinkedList.Node temp = ll.head, iNode = null; 
        int i = 0;
            if(i==index)iNode = temp;
            temp.value = array[i];
   = Node(); 
            temp =;
        if(index==i)iNode = temp;
        temp.value = array[i]; = iNode;
        return ll;
    public static void main(String[] args){
        Integer[] array = {1,2,3,4,5,6,7,8,9,10,11};
        LinkedList<Integer> ll = makeCycleList(array, 3);


public class LinkedList <T>{
    Node head;
    public class Node{
        Node next;
        T value;
        Node(T value){this.value = value;}
        public boolean equals(Object o){ 
            return this.hashCode() == o.hashCode();
        public int hashCode(){
            return value.hashCode();
        public String toString(){
            return value.toString();
    public void add(T value){
        if(head==null){head = new Node(value); return;}
        Node t = head;
        while(!=null){t =;} = new Node(value); 
    public boolean isEmpty(){
        return head==null;
    public String toString(){
        StringBuilder bf = new StringBuilder();
        Node t = head;
            bf.append(t.value).append(" -> ");
            t =;
        bf.delete(bf.length()-4, bf.length());
        return bf.toString();



No comments:

Post a Comment