原文:

给定正整数的双链表。 任务是按照相反的顺序打印给定的双链表数据。

示例

input: list = 1 <=> 2 <=>  3 <=>  4 <=>  5
output: 5  4  3  2  1
input: 10 <=> 20 <=> 30 <=> 40
output: 40  30  20  10

方法

  • 用指针指向双链表的头部。

  • 现在,开始遍历链表,直到最后。

  • 到达最后一个节点后,开始向后移动并同时打印该节点->数据。

下面是上述方法的实现:

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 << tail->data << " "; 
        tail = tail->prev; 
    } 
    cout << tail->data << endl; 
} 
/* utility functions */
// function to insert a node at the  
// beginging of the doubly linked list  
void push(struct node** head_ref, int new_data) 
{ 
    // allocate node  
    struct node* new_node = (struct node*)malloc(sizeof(struct node)); 
    // put in the data  
    new_node->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 << "linked list elements in reverse order : " << endl; 
    reverseprint(&head); 
    return 0; 
} 

java

// java program to print doubly  
// linked list in reverse order  
class sol 
{ 
// doubly linked list node  
static class node 
{  
    int data;  
    node next;  
    node prev;  
};  
// function to print nodes of doubly  
// linked list in reverse order  
static void reverseprint( node head_ref)  
{  
    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) 
    {  
        system.out.print( tail.data   " ");  
        tail = tail.prev;  
    }  
    system.out.println( tail.data );  
}  
// utility functions / 
// function to insert a node at the  
// beginging of the doubly linked list  
static node push( node head_ref, int new_data)  
{  
    // allocate node  
    node new_node = new node();  
    // put in the data  
    new_node.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;  
    return head_ref; 
}  
// driver code  
public static void main(string args[]) 
{  
    // start with the empty list  
    node head = null;  
    // let us create a sorted linked list  
    // to test the functions  
    // created linked list will be 10.8.4.2  
    head = push(head, 2);  
    head = push(head, 4);  
    head = push(head, 8);  
    head = push(head, 10);  
    system.out.print( "linked list elements in reverse order : " );  
    reverseprint(head);  
}  
} 
// this code is contributed by arnab kundu  

python3

# python3 program to print doubly  
# linked list in reverse order  
import math 
# doubly linked list node 
class node:  
    def __init__(self, data):  
        self.data = data  
        self.next = none
# function to print nodes of doubly  
# linked list in reverse order  
def reverseprint(head_ref): 
    tail = head_ref 
    # traversing till tail of the linked list 
    while (tail.next != none): 
        tail = tail.next
    # traversing linked list from tail 
    # and pring the node.data 
    while (tail != head_ref): 
        print(tail.data, end = " ") 
        tail = tail.prev 
    print(tail.data) 
# utility functions  
# function to insert a node at the  
# beginging of the doubly linked list  
def push(head_ref, new_data): 
    # allocate node  
    new_node = node(new_data) 
    # put in the data  
    new_node.data = new_data 
    # since we are adding at the beginning,  
    # prev is always none  
    new_node.prev = none
    # link the old list off the new node  
    new_node.next = head_ref 
    # change prev of head node to new node  
    if (head_ref != none): 
        head_ref.prev = new_node 
    # move the head to po to the new node  
    head_ref = new_node 
    return head_ref 
# driver code 
if __name__=='__main__': 
    # start with the empty list  
    head = none
    # let us create a sorted linked list 
    # to test the functions  
    # created linked list will be 10.8.4.2  
    head = push(head, 2)  
    head = push(head, 4)  
    head = push(head, 8)  
    head = push(head, 10) 
    print("linked list elements in reverse order : ") 
    reverseprint(head) 
# this code is contributed by srathore 

c

// c# program to print doubly  
// linked list in reverse order 
using system;      
class sol 
{ 
// doubly linked list node  
public class node 
{  
    public int data;  
    public node next;  
    public node prev;  
};  
// function to print nodes of doubly  
// linked list in reverse order  
static void reverseprint( node head_ref)  
{  
    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) 
    {  
        console.write( tail.data   " ");  
        tail = tail.prev;  
    }  
    console.writeline( tail.data );  
}  
// utility functions / 
// function to insert a node at the  
// beginging of the doubly linked list  
static node push( node head_ref, int new_data)  
{  
    // allocate node  
    node new_node = new node();  
    // put in the data  
    new_node.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;  
    return head_ref; 
}  
// driver code  
public static void main(string []args) 
{  
    // start with the empty list  
    node head = null;  
    // let us create a sorted linked list  
    // to test the functions  
    // created linked list will be 10.8.4.2  
    head = push(head, 2);  
    head = push(head, 4);  
    head = push(head, 8);  
    head = push(head, 10);  
    console.write( "linked list elements in reverse order : " );  
    reverseprint(head);  
}  
} 
// this code contributed by rajput-ji 

输出

linked list elements in reverse order : 
2 4 8 10


如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。