• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linked List Recursion  RSS feed

 
Samir Raut
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Confusion:

In the reverseTest method which is recursive after returning first L value from this line


we pass the value to remainingNode in this line

Node remainingNode = reverseTest(L.next);

then we copy it to cur variable

when we reach the line cur.next = L;
it updates the cur.next with L

but it also updates

I don't understand. How?
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please, use code tags when posting (click → UseCodeTags).
I have added them for you this time.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the question. Does reverseTest() work? Have you posted all the necessary code (for instance, Node and a rewritten LinkedList class are missing).

If reverseTest() works, I would need to see the remaining code. If it doesn't work, that's probably because it is updating the local variable cur but is not saving it anywhere.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are to write your own LinkedList class, how about a boolean member called 'forward'.
If you have an instance linli, and forward == true ( ) then linli.next() acts as usual,
Calling linli.reverse() flips 'forward' and then linli.next() gives the previous element.

Well, keeps you off the street.

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!