Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polynomial Linked List Reverse

 
mike fusc
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I add to add together polynomials and got through it but the solution comes out backwards. How would I go about reversing the Nodes in the lsit

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4410
251
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you need to iterate from the last node to the first node by backtracking. To ShowSomeEffort, can you show us what have you done up to now?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the output is wrong then you should try to fix the problem and not solve its symptoms. If it's not wrong then you can reverse the List with Collections.reverse().
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And otherwise there's still ListIterator:
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the small difference that Collections.reverse actually changes your List while the ListIterator only changes your "view" on that List.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to iterate the List, push all the elements onto a stack, then pop them and add them to a new List?
 
Stephan Mueller
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is it possible to iterate the List, push all the elements onto a stack, then pop them and add them to a new List?


Is that a rethorical question ?
At least, it lead me to find http://download.oracle.com/javase/6/docs/api/java/util/Stack.html , which I haven't seen by now. And with JavaSE6, erm 5, it's
a generic Class! whoohooo
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic