Results 1 to 5 of 5
  1. #1
    hamed is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default Doubly linked list, reverse()

    Hi. I have a Doubly-linked list and i need to reverse it. I'm not sure if the method that i wrote works. It's a lil bit tricky. Would you plz correct this method if there is any thing wrong with this code?
    Appreciate it

    public void reverse()
    {
    if (first == null)
    return;

    DoubleNode previous = first;
    DoubleNode current = first.next;
    first.next = null;
    while (current != null)
    {
    DoubleNode next = current.next;
    current.next = previous;
    previous = current;
    current = next;
    }
    first = previous;
    }

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Looks like it would work to me, just write some code and test away. If there are any errors, post back.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    hamed is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks guys.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Why don't you take advantage of the fact you have a doubly linked list? All you would need is an additional variable to tell you what direction you're going, then all you'd need to do is change the variable that points to the first element and change direction:
    Java Code:
    public class DoublyLinkedList {
      Node first, current;
      boolean forward;
      //constructors... methods...
      
      private Node next() {
        if(forward) return current.next();
        else return current.previous();
      }
      
      public void reverse() {
        while(true) {
          if(next() == null) {
            first = current;
            forward = !forward;
            return;
          }
          current = next(); 
        }
      }
    }
    Also note that this example will produce errors if used like this, you'd have to check if the current element is null etc etc.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    hamed is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Sorted Doubly linked List
    By student2889 in forum New To Java
    Replies: 1
    Last Post: 10-14-2010, 10:05 AM
  2. Doubly Linked List
    By matin1234 in forum New To Java
    Replies: 0
    Last Post: 06-02-2010, 05:58 AM
  3. doubly linked list insert
    By ineedhelpwithjava in forum Advanced Java
    Replies: 1
    Last Post: 03-20-2009, 02:05 PM
  4. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM
  5. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:30 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
  •