Java - linked list in reverse

how to reverse my list whithout doubly linked list...

Page 1 of 1

3 Replies - 17336 Views - Last Post: 20 November 2010 - 03:58 PM Rate Topic: -----

#1 Guest_uklearner*


Reputation:

Java - linked list in reverse

Posted 20 November 2010 - 05:08 AM

I have my linked list and I already managed how to print the list, but now I am sttrugling to print the list in reverse order... I cannot implement a doubly linked list or attempt to store data in reverse order as it is been read from the file. I have to use linked list queue after all objects have been added. How do I do that?
Is This A Good Question/Topic? 0

Replies To: Java - linked list in reverse

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Java - linked list in reverse

Posted 20 November 2010 - 08:03 AM

If you are allowed to use a doubly linked list, then that is probably the easiest way to that. Give each node 2 pointers: one to the next node and one to the previous node. You may or may not want to make the head and tail nodes connect, based on what you want. For more on this concept, I'd recommend reading macosxnerd101's tutorial here:
http://www.dreaminco...-list-tutorial/

However, if you cannot use a doubly linked list, I suggest going through the LinkedList and pushing each item onto a Stack. Once all the elements are on the Stack, then pop them off and print )they will be reversed).
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10809
  • View blog
  • Posts: 40,294
  • Joined: 27-December 08

Re: Java - linked list in reverse

Posted 20 November 2010 - 03:45 PM

See my snippet on Recursively reverse printing a Linked List.
Was This Post Helpful? 2
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Java - linked list in reverse

Posted 20 November 2010 - 03:58 PM

View Postmacosxnerd101, on 20 November 2010 - 04:45 PM, said:



Clever. Forgot about that recursive feature! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1