A doubly linked list DLL is a linked list with nodes that po

A doubly linked list (DLL) is a linked list with nodes that point both forward and backward. Here\'s an example:

Here\'s a DLL node definition:

The next of the last node will be null, and the prev of the first node will be null.

implement a method to reverse the sequence of items in a DLL. Your code should NOT create any new nodes - it should simply resequence the original nodes. The method should return the front of the resulting list.

Solution

// Giving the function along with a sample program!

// DLLNode.java

public class DLLNode {
   public String data;
   public DLLNode prev, next;
   public DLLNode(String data, DLLNode next, DLLNode prev) {
       this.data = data; this.next = next; this.prev = prev;
   }
}

// DLL.java

public class DLL {
   public static DLLNode reverse(DLLNode front) {
       DLLNode temp = null;
       DLLNode current = front;
  
       while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
if(current.prev != null) current = current.prev;
else break;
}
      
       return current;
   }

   public static void display(DLLNode front){
       DLLNode temp = front;
       while(temp != null){
           System.out.print(temp.data + \" \");
           temp = temp.next;
       }
       System.out.println();
   }
   public static void main(String args[]){
       DLLNode node1 = new DLLNode(\"1\", null, null);
       DLLNode node2 = new DLLNode(\"2\", null, node1);
       DLLNode node3 = new DLLNode(\"3\", null, node2);
       DLLNode node4 = new DLLNode(\"4\", null, node3);
       node1.next = node2;
       node2.next = node3;
       node3.next = node4;
       display(node1);
       display(reverse(node1));
   }
}

A doubly linked list (DLL) is a linked list with nodes that point both forward and backward. Here\'s an example: Here\'s a DLL node definition: The next of the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site