Bloomberg Interview Question

how will you find a circular link list which has a loop in it.

Interview Answers

Anonymous

Jan 3, 2016

use 2 iterator algo. or pass the linked list elements into a Java set, it will fail to add elements twice if there is a loop

Anonymous

Jan 28, 2016

boolean hasLoop(Node first) { Node slow = first; Node fast = first; while(fast != null && fast.next != null) { slow = slow.next; // 1 hop fast = fast.next.next; // 2 hops if(slow == fast) // fast caught up to slow, so there is a loop return true; } return false; // fast reached null, so the list terminates }