Yahoo Interview Question: How can you tell if a linked ... | Glassdoor

Interview Question

Senior Software Engineer Interview Carlsbad, CA

How can you tell if a linked list is circular (goes on

  forever because it connects to a previous node)?

Interview Answer

3 Answers


Have two cursors, one moving one node at a time and the other moving two at a time. If the two nodes ever equal each other, it's a circular linked list.

Interview Candidate on Sep 4, 2013

Why not just having 2 pointers - head and tail of the list. If tail->next points to head - then list is circular.

Eugen on Aug 3, 2015

Ah ok, seems like you get only the head pointer as an argument. The more so it could be a cyclical list, where the tail points to an arbitrary node in the list, which may not be the head.

Eugene on Aug 3, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.