Results 1 to 6 of 6
Thread: LinkedList Reversing Help.
- 11-15-2011, 07:00 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 22
- Rep Power
- 0
LinkedList Reversing Help.
Can anyone please this reverse code to me? I really don't understand how this code works...Java Code:Node first, last; public class Node{ String value; Node next; public Node(String value, Node next){ this.value = value; this.next = next; } } public void reverse(){ if(first == last){ return; } Node newlist = first; last = first; Node current = first; Node next = current.next; while(next != null){ current = next; next = current.next; current.next = newlist; newlist = current; } first = current; last.next = null; }
- 11-15-2011, 08:18 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Re: LinkedList Reversing Help.
Post the code again adding a comment before every line with what you think it does. We will correct you where you are mistaken.
- 11-15-2011, 08:36 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 22
- Rep Power
- 0
Re: LinkedList Reversing Help.
To be frankly saying , I just don't understand part starting from while loop.
I know that last = first because the first thing must be in the last but other than that seriously..
- 11-15-2011, 08:42 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Re: LinkedList Reversing Help.
Then, seriously, no one can help you. You can only help yourself. Do not think about what the "task" of the method is, look only at each line of code and say what it does, without regard, at first, to the task of the method.
Before that, however, describe what "first" and "last" are in this class and what "next" is in the Node class.
- 11-15-2011, 09:14 AM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Re: LinkedList Reversing Help.
Just so you know, this is not "to be mean to you". This is a general skill that a programmer must have and I am simply trying to develop that in you. You must at least try. Even if your assumptions are wrong, post them.
- 11-15-2011, 10:28 AM #6
Member
- Join Date
- Sep 2011
- Posts
- 22
- Rep Power
- 0
Re: LinkedList Reversing Help.
You are indeed right. Thanks for your advice, after struggling with this code for approx. 2 hours I was able to understand the code.
I think one thing that I could not understand was the
current.next = newList
this is very important because by doing this current can go back of the current.next
and then
current becomes the new list by
newlist = current;
Thanks!
Similar Threads
-
reversing digits
By mamoonrizwan in forum Advanced JavaReplies: 12Last Post: 08-13-2011, 09:07 AM -
Reversing the String
By Inaam in forum New To JavaReplies: 1Last Post: 03-30-2009, 08:35 PM -
Reversing
By whosadork in forum New To JavaReplies: 14Last Post: 11-06-2008, 04:29 AM -
Reversing String
By mew in forum New To JavaReplies: 4Last Post: 12-02-2007, 09:42 PM -
reversing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks