aspose file tools*
The moose likes Java in General and the fly likes singly inked list traversing in the reverse direction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "singly inked list traversing in the reverse direction" Watch "singly inked list traversing in the reverse direction" New topic
Author

singly inked list traversing in the reverse direction

manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
I have a single linked list of some premitive values. Size of the list is unknown. If I want to print the contents in the reverse order without using any datastructure, is it possible? If yes, how?


love your job and not your company;
...because you never know when your company will stop loving you!
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 570
    
    7

Yes,

The most easy way is to create a recursive print method that calls itself with the next node and then prints the current node data value.
[ September 11, 2006: Message edited by: Steve Fahlbusch ]
manoj r patil
Ranch Hand

Joined: Jun 06, 2002
Posts: 181
But this will print the output in the forward order... How can I get it in the reverse order?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by patil manoj:
But this will print the output in the forward order...


No, it won't. Read the suggestion carefully and try it with a small example on paper - it really prints in reverse order.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: singly inked list traversing in the reverse direction