Symantec

  www.symantec.com
  www.symantec.com

Interview Question

Software Engineer Interview Culver City, CA

how to detect cycle in linked list

Tags:
linked list cycle
Answer

Interview Answer

3 Answers

1

use the fast and slow pointers to detect cycles: one pointer move 1 node at a time, one pointer moves 2 nodes at a time, if there is a cycle then these pointers will meet at some point

Interview Candidate on Jul 10, 2011
1

I would create a HashSet to store the set of references (or pointers). Traverse the LL and insert references in the set. Before inserting reference of a node, check to see if the set contains the reference already. If it does, then the LL has a cycle.

Hari on Sep 23, 2012
0

This is called hare and tortoise prob where you use 2 pointers.1 moves slow and other moves fast along the linkedlist. If meet they meet then it implies that the linkedlist is cyclic.

Ak on Sep 29, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.