How to check if linked list contains loop in java?



How to check if linked list contains loop in java?..

Answer / Rohit Kumar Bhadani

To check if a singly linked list contains a cycle, you can use Floyd's cycle-finding algorithm (also known as the tortoise and hare approach):n
1. Initialize two pointers: slow and fast, moving one step at a time (slow) and two steps at a time (fast).
2. If the fast pointer reaches null or the end of the list before the slow pointer catches up to it, then the list has no cycle.
3. When the fast pointer catches up with the slow pointer, they will meet at the beginning of the loop. This is where you can find the start of the cycle.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

hoe can u call a constructor of a private classs to other inherited claa??

1 Answers  


what is the difference between future and callable interface in java?

1 Answers  


What is the Dictionary class?

1 Answers  


What is the base class in java from which all classes are derived?

1 Answers  


wahts is mean by thread?

22 Answers   HCL, TCS,


What is an empty class? What functionality does it offer in Java?

1 Answers   Deloitte, EXL, JPMorgan Chase,


What are java packages? What's the significance of packages?

1 Answers  


How OOPS concept is achieved in Java?

6 Answers   Cognizant, JPMorgan Chase, Xavient,


What is the difference between iterator and list iterator?

1 Answers  


What is a type parameter in java?

1 Answers  


How can you reverse a string?

8 Answers  


What's a method in programming?

1 Answers  


Categories