• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singly inked list traversing in the reverse direction

 
manoj r patil
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this will print the output in the forward order... How can I get it in the reverse order?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic