views:

916

answers:

5

how can I find last node of a circular linked list whose size I dont know and the last node points to any other node except first node of the linked list?

+4  A: 

By definition, if a node does not point to the first node of a circular linked list,
it is not the last node.

Can you elaborate here?

nik
I think this is because we call it "Circular" Isn't it?
Jonathan
It's pretty clear what he means: a linked list where the "last node" points to some other non-first node in the list, making it a Q-shape. No idea why or how you would get a list like that though.
Sean Nyman
You can form `Q`s and pretzels. The circular list does not end till you reach the head. And, if you conclude an end to the list w/o reaching the head, the list is not circular anymore.
nik
In which case, it would be good to get a clear description of the list for an appropriate answer.
nik
Actually, unless you had a list where nodes could point to multiple successors, you couldn't have a pretzel. Anyway, while it's not strictly circular, if you have a clearly defined head, then your clearly defined end is the last new node you get to while traversing the list starting from the head. I think calling a Q shaped list "circular" and then qualifying it by saying it has a tail or the phrasing in the OP is valid. I do agree that more detail would be good though. I also get the feeling this is a homework or interview question.
Sean Nyman
A: 

Maybe add parameter to nodes of the list which tells you if you at end? I think, it wouldn't be problem.

Otherwise, you can remember nodes you already visted. When the next node is already visited, you are at the end.

iyo
+1  A: 

A strange list... why would you ever need something like this? But anyway...

You can simply iterate over all nodes, and stop as soon as the next node would be one you have already visited. The current node will then be your answer.

You need some way to keep track of which nodes have been visited. Add a boolean flag to each node, or use some kind of set data type with fast insertion and lookup (e.g. a hash set).

Thomas
+3  A: 

One algorithm that can be used for this is the Floyd cycle algorithm.

Also, see this question.

CAdaker
Thank you for the pointer to Floyd's algorithm. Very nice. I saw a puzzle about this in Steve Skiena's algorithms book, and wondered what the best answer was.http://www.amazon.com/Algorithm-Design-Manual-Steven-Skiena/dp/1848000693/
khedron
A: 

The Floyd cycle algorithm won't give the last element of the list. It will only tell if there is a cycle or not.

The definition of the last one would be that, while traversing the list in a sequential scan starting from the first one, all elements before it and the last one aren't seen before (pointer value). The after last one will be the first element that has already been seen in this sequential scan.

An easy solution is to flag visited elements so an element already seen is easily detected. The flag may be intrusive, i.e. by changing a bit in the element, or external by using a hash table to store pointer values.

Since we need to be able to test if an element has already been visited, I don't see another solution.

chmike