Reverse a doubly Linked List
  • node
  • doublylinkedlist
  • linkedlist
  • stackqueuelinkedlist
  • reverse
  •   
  • Posted: 5 years ago
  • Edit
  • answers (1)
  • views (5221)

Write a function to reverse a doubly Linked List.


Posted Answers


  • Swap head and tail pointer.
  • For each node of the doubly linked list, swap the previous and next pointer.

public static void reverseDoublyLL(LinkedListNode head, LinkedListNode tail){

/* Swap head and tail pointer */
LinkedListNode temp = head;
head = tail;
tail = temp;

/* Create a node pointing to head */
LinkedListNode current = head;

while(current != null){
/* Swap previous and next pointer of each node */
temp = current.next;
current.next = current.prev;
current.prev = temp;

}

}

You need to Sign In to post your solution.