Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Algorithm - Interview Questions and Answers for 'Cyclic linkedlist' - 1 question(s) found - Order By Newest | ||||
| ||||
Ans. public class LinkedList { Node start = null; Node head = null; class Node { Integer body; Node nextNode; Node(Integer value) { body = value; } } public static void main(String[] args) { LinkedList ll = new LinkedList(); ll.addNodeToEnd(5); ll.addNodeToEnd(10); ll.addNodeToEnd(15); ll.traverse(); if(checkIfLoop(l1)){ System.out.println("There is a Loop"); } else { System.out.println("No Loop"); } } private boolean checkifLoop(Test l1) { Node slow = start; Node fast = start; Node faster = start; while(slow != null ) { fast = slow.nextNode; faster = fast.nextNode; if(slow == fast || slow == faster) { return true; } slow = slow.nextNode; } return false; } } Complexity of this algorithm is O(n) | ||||
![]() | ||||
![]() ![]() ![]() | ||||
![]() | ||||
![]() | ||||
![]() | ||||
![]() | ||||
![]() Collection<Collection> collection = new LinkedList<LinkedList>(); | ||||
![]() | ||||
![]() | ||||
![]() | ||||
![]() | ||||
![]() | ||||
![]() | ||||