How do you reverse a traverse backwards in a doubly list?

Reverse 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 head pointer in the end.



Print Doubly Linked list in Reverse Order

Given a doubly-linked list of positive integers. The task is to print the given doubly linked list data in reverse order.

Examples:

Input: List = 1 2 3 4 5 Output: 5 4 3 2 1 Input: 10 20 30 40 Output: 40 30 20 10

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

  • Take a pointer to point to head of the doubly linked list.
  • Now, start traversing through the linked list till the end.
  • After reaching last node, start traversing in backward direction and simultaneously print the node->data.

Below is the implementation of the above approach:

C++




// C++ Program to print doubly

// linked list in reverse order

#include

using namespace std;

// Doubly linked list node

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

// Function to print nodes of Doubly

// Linked List in reverse order

void reversePrint[struct Node** head_ref]

{

struct Node* tail = *head_ref;

// Traversing till tail of the linked list

while [tail->next != NULL] {

tail = tail->next;

}

// Traversing linked list from tail

// and printing the node->data

while [tail != *head_ref] {

cout data prev;

}

cout data data = new_data;

// since we are adding at the beginning,

// prev is always NULL

new_node->prev = NULL;

// link the old list off the new node

new_node->next = [*head_ref];

// change prev of head node to new node

if [[*head_ref] != NULL]

[*head_ref]->prev = new_node;

// move the head to point to the new node

[*head_ref] = new_node;

}

// Driver Code

int main[]

{

// Start with the empty list

struct Node* head = NULL;

// Let us create a sorted linked list

// to test the functions

// Created linked list will be 10->8->4->2

push[&head, 2];

push[&head, 4];

push[&head, 8];

push[&head, 10];

cout data.

How do I print a linked list backwards?

Printing Linked list in Reverse order using Recursion is very easy. STEP 1: Recursively iterate the linked list till you not reach null that is the end of the linked list. STEP 2: When you reach null, return. STEP 3: While returning from each recursive call, Print the Node data and you are done.

How do you reverse a traverse linked list in Java?

A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in a LinkedList. The method hasPrevious[ ] in ListIterator returns true if there are more elements in the LinkedList while traversing in the reverse direction and false otherwise.

1. Iterative Solution

The idea is simple – Traverse the list and swap next and prev pointers for each node. Finally, update head pointer to point to the last node.

Following is the C, Java, and Python program that demonstrates it:

Introduction to Doubly Linked List

A doubly linked list is a linear data structure which is a linked list which contains an extra pointer to store the address of the previous node. It is a variation of singly linked list. In singly linked list, only forward traversal is possible, whereas in doubly linked list, forward and backward traversal is possible.

Video liên quan

Bài mới nhất

Chủ Đề