It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Finding Tail Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding Tail Reference" Watch "Finding Tail Reference" New topic
Author

Finding Tail Reference

Christopher Beech
Ranch Hand

Joined: Feb 08, 2006
Posts: 40
How would you find the tail reference of a linked list???

I'm sure that you would need to do a traversal. The tail reference would be the last node where the reference would be null.



Not sure what code to put after the traversal. Also, this isn't for homework.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I'm too lazy to check this, but how about...

[ March 28, 2006: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Seems easier to read...

If you do this a lot and you can prove it's a bottleneck, can you modify your list to maintain a tail variable? I think that's fairly common.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Yes, a tail reference in a linked list is common, and helps out performance a bit since a list is not random access.

If you don't/can't have one, traversing is your only option to get the data in the end of the linked list.
[ March 28, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding Tail Reference