Open In App

Javascript Program For Reversing A Doubly Linked List

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Doubly Linked List, the task is to reverse the given Doubly Linked List.

See below diagrams for example. 

(a) Original Doubly Linked List  

(b) Reversed Doubly Linked List  

Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the head pointer in the end.

Javascript




<script>
// Javascript program to reverse a
// doubly linked list
var head;
 
class Node
{
    constructor(val)
    {
        this.data = val;
        this.prev = null;
        this.next = null;
    }
}
     
/* Function to reverse a Doubly Linked List */
function reverse()
{
    var temp = null;
    var current = head;
     
    /* Swap next and prev for all nodes of
       doubly linked list */
    while (current != null)
    {
        temp = current.prev;
        current.prev = current.next;
        current.next = temp;
        current = current.prev;
    }
 
    /* Before changing head, check for the cases
       like empty list and list with only one node */
    if (temp != null)
    {
        head = temp.prev;
    }
}
 
// UTILITY FUNCTIONS
/* Function to insert a node at the beginning
   of the Doubly Linked List */
function push(new_data)
{
    // Allocate node
    var new_node = new Node(new_data);
    
    /* Since we are adding at the beginning,
       prev is always NULL */
    new_node.prev = null;
 
    /* Link the old list of the new node */
    new_node.next = head;
 
    /* Change prev of head node to new node */
    if (head != null)
    {
        head.prev = new_node;
    }
 
    /* Move the head to point to the new node */
    head = new_node;
}
 
/* Function to print nodes in a given doubly
   linked list This function is same as
   printList() of singly linked list */
function printList(node)
{
    while (node != null)
    {
        document.write(node.data + " ");
        node = node.next;
    }
}
 
/* Let us create a sorted linked list to test
   the functions Created linked list will be
   10->8->4->2 */
push(2);
push(4);
push(8);
push(10);
 
document.write(
"Original linked list <br/>");
printList(head);
 
reverse();
document.write("<br/>");
document.write("The reversed Linked List is <br/>");
printList(head);
// This code is contributed by gauravrajput1
</script>


Output: 

Original linked list 
10 8 4 2 
The reversed Linked List is 
2 4 8 10

Time Complexity: O(N), where N denotes the number of nodes in the doubly linked list.
Auxiliary Space: O(1)
We can also swap data instead of pointers to reverse the Doubly Linked List. Method used for reversing array can be used to swap data. Swapping data can be costly compared to pointers if the size of the data item(s) is more.
Please write comments if you find any of the above codes/algorithms incorrect, or find better ways to solve the same problem.

Method 2: 

The same question can also be done by using Stacks. 

Steps:

  1. Keep pushing the node’s data in the stack. -> O(n)
  2. The keep popping the elements out and updating the Doubly Linked List

Javascript




<script>
// Javascript program to reverse a doubly
// linked list
class Node
{
    constructor(d)
    {
        this.data = d;
        this.next = this.prev = null;
    }
}
 
let head;
 
// Function to reverse a Doubly
// Linked List using Stacks
function reverse()
{
    let stack = [];
    let temp = head;
     
    while (temp != null)
    {
        stack.push(temp.data);
        temp = temp.next;
    }
     
    // Added all the elements sequence
    // wise in the stack
    temp = head;
     
    while (temp != null)
    {
        temp.data = stack.pop();
        temp = temp.next;
    }
     
    // Popped all the elements and the
    // added in the linked list,
    // which are in the reversed order.
}
 
// UTILITY FUNCTIONS
// Function to insert a node at the
// beginning of the Doubly Linked List
function push(new_data)
{
     
    // Allocate node
    let new_node = new Node(new_data);
 
    /* Since we are adding at the beginning,
       prev is always NULL */
    new_node.prev = null;
 
    // Link the old list of the new node
    new_node.next = head;
 
    /* Change prev of head node to
       new node */
    if (head != null)
    {
        head.prev = new_node;
    }
 
    // Move the head to point to the
    // new node
    head = new_node;
}
 
// Function to print nodes in a given
// doubly linked list. This function
// is same as printList() of singly
// linked list
function printList(node)
{
    while (node != null)
    {
        document.write(node.data + " ");
        node = node.next;
    }
}
 
// Driver Code
 
// Let us create a sorted linked list
// to test the functions Created linked
// list will be 10->8->4->2
push(2);
push(4);
push(8);
push(10);
 
document.write(
"Original linked list <br>");
printList(head);
 
reverse();
document.write("<br>");
document.write(
"The reversed Linked List is <br>");
printList(head);
// This code is contributed by rag2127
</script>


Output:

Original linked list 
10 8 4 2 
The reversed Linked List is 
2 4 8 10

Time Complexity: O(N)
Auxiliary Space: O(N)

In this method, we traverse the linked list once and add elements to the stack, and again traverse the whole for updating all the elements. The whole takes 2n time, which is the time complexity of O(n).

Please refer complete article on Reverse a Doubly Linked List for more details!



Previous Article
Next Article

Similar Reads

C++ Program For Reversing A Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example.  (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change th
5 min read
C Program For Reversing A Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the
3 min read
Java Program For Reversing A Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the
5 min read
Python Program For Reversing A Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example.  (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change th
4 min read
Javascript Program For Printing Reverse Of A Linked List Without Actually Reversing
Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1-&gt;2-&gt;3-&gt;4, then output should be 4-&gt;3-&gt;2-&gt;1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorithm: printReverse(head) 1. call print reverse for h
2 min read
Javascript Program For Reversing Alternate K Nodes In A Singly Linked List
Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm. Example: Inputs: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;9-&gt;NULL and k = 3 Output: 3-&gt;2-&gt;1-&gt;4-&gt;5-&gt;6-&gt;9-&gt;8-&gt;7-&gt;NULL. Method 1 (Process 2k node
6 min read
Javascript Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).  Example:  Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL  Recommended: Please solve it on "PRACTICE" first, before moving on to the so
3 min read
Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 3 Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL. Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 5 Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-
3 min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
When is Doubly Linked List more Efficient than Singly Linked List?
Did you know there are some cases where a Doubly Linked List is more efficient than a Singly Linked List, even though it takes more memory compared to a Singly Linked List? What are those Cases? Well, we will discuss that in the following article, But first, let's talk about Singly and linked lists: What is a Singly Linked List?A singly linked list
4 min read
Practice Tags :