Results 1 to 6 of 6
  1. #1
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default LinkedList Reversing Help.

    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;
    	}
    Can anyone please this reverse code to me? I really don't understand how this code works...

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  3. #3
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default 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..

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default 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.

  6. #6
    gomdohri is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default 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

  1. reversing digits
    By mamoonrizwan in forum Advanced Java
    Replies: 12
    Last Post: 08-13-2011, 10:07 AM
  2. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 09:35 PM
  3. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 05:29 AM
  4. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 10:42 PM
  5. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •