原文:

给定一个链表,使用递归函数反向打印。 例如,如果给定的链表为1 -> 2 -> 3 -> 4,则输出应为4 -> 3 -> 2 -> 1

注意,问题仅在于打印反面。 要反转列表本身,请参阅。

难度级别:菜鸟

算法

printreverse(head)
  1\. call print reverse for head->next
  2\. print head->data

实现

c

// c   program to print reverse of a linked list  
#include  
using namespace std; 
/* link list node */
class node  
{  
    public: 
    int data;  
    node* next;  
};  
/* function to reverse the linked list */
void printreverse(node* head)  
{  
    // base case  
    if (head == null)  
    return;  
    // print the list after head node  
    printreverse(head->next);  
    // after everything else is printed, print head  
    cout << head->data << " ";  
}  
/*utility functions*/
/* push a node to linked list. note that this function  
changes the head */
void push(node** head_ref, char new_data)  
{  
    /* allocate node */
    node* new_node = new node(); 
    /* put in the data */
    new_node->data = new_data;  
    /* link the old list off the new node */
    new_node->next = (*head_ref);  
    /* move the head to pochar to the new node */
    (*head_ref) = new_node;  
}  
/* driver code*/
int main()  
{  
    // let us create linked list 1->2->3->4  
    node* head = null;  
    push(&head, 4);  
    push(&head, 3);  
    push(&head, 2);  
    push(&head, 1);  
    printreverse(head);  
    return 0;  
}  
// this code is contributed by rathbhupendra 

c

// c program to print reverse of a linked list 
#include 
#include 
/* link list node */
struct node 
{ 
    int data; 
    struct node* next; 
}; 
/* function to reverse the linked list */
void printreverse(struct node* head) 
{ 
    // base case   
    if (head == null) 
       return; 
    // print the list after head node 
    printreverse(head->next); 
    // after everything else is printed, print head 
    printf("%d  ", head->data); 
} 
/*utility functions*/
/* push a node to linked list. note that this function 
  changes the head */
void push(struct node** head_ref, char new_data) 
{ 
    /* allocate node */
    struct node* new_node = 
            (struct node*) malloc(sizeof(struct node)); 
    /* put in the data  */
    new_node->data  = new_data; 
    /* link the old list off the new node */
    new_node->next = (*head_ref);    
    /* move the head to pochar to the new node */
    (*head_ref)    = new_node; 
}  
/* driver program to test above function*/
int main() 
{ 
    // let us create linked list 1->2->3->4 
    struct node* head = null;     
    push(&head, 4); 
    push(&head, 3); 
    push(&head, 2); 
    push(&head, 1); 
    printreverse(head); 
    return 0; 
} 

java

// java program to print reverse of a linked list 
class linkedlist 
{ 
    node head;  // head of list 
    /* linked list node*/
    class node 
    { 
        int data; 
        node next; 
        node(int d) {data = d; next = null; } 
    } 
    /* function to print reverse of linked list */
    void printreverse(node head) 
    { 
        if (head == null) return; 
        // print list of head node 
        printreverse(head.next); 
        // after everything else is printed 
        system.out.print(head.data " "); 
    } 
    /* utility functions */
    /* inserts a new node at front of the list. */
    public void push(int new_data) 
    { 
        /* 1 & 2: allocate the node & 
                  put in the data*/
        node new_node = new node(new_data); 
        /* 3\. make next of new node as head */
        new_node.next = head; 
        /* 4\. move the head to point to new node */
        head = new_node; 
    } 
    /*driver function to test the above methods*/
    public static void main(string args[]) 
    { 
        // let us create linked list 1->2->3->4 
        linkedlist llist = new linkedlist(); 
        llist.push(4); 
        llist.push(3); 
        llist.push(2); 
        llist.push(1); 
        llist.printreverse(llist.head); 
    } 
} 
/* this code is contributed by rajat mishra */

python3

# python3 program to print reverse 
# of a linked list  
# node class  
class node:  
    # constructor to initialize 
    # the node object  
    def __init__(self, data):  
        self.data = data  
        self.next = none
class linkedlist:  
    # function to initialize head  
    def __init__(self):  
        self.head = none
    # recursive function to print  
    # linked list in reverse order 
    def printrev(self, temp): 
        if temp: 
            self.printrev(temp.next) 
            print(temp.data, end = ' ') 
        else: 
            return
    # function to insert a new node  
    # at the beginning  
    def push(self, new_data):  
        new_node = node(new_data)  
        new_node.next = self.head  
        self.head = new_node  
# driver code 
llist = linkedlist()  
llist.push(4)  
llist.push(3)  
llist.push(2)  
llist.push(1)  
llist.printrev(llist.head) 
# this code is contributed by vinay kumar(vinaykumar71) 

c

// c# program to print reverse of a linked list 
using system; 
public class linkedlist 
{ 
    node head; // head of list 
    /* linked list node*/
    class node 
    { 
        public int data; 
        public node next; 
        public node(int d)  
        { 
            data = d; next = null; 
        } 
    } 
    /* function to print reverse of linked list */
    void printreverse(node head) 
    { 
        if (head == null) return; 
        // print list of head node 
        printreverse(head.next); 
        // after everything else is printed 
        console.write(head.data   " "); 
    } 
    /* utility functions */
    /* inserts a new node at front of the list. */
    public void push(int new_data) 
    { 
        /* 1 & 2: allocate the node & 
                put in the data*/
        node new_node = new node(new_data); 
        /* 3\. make next of new node as head */
        new_node.next = head; 
        /* 4\. move the head to point to new node */
        head = new_node; 
    } 
    /*driver function to test the above methods*/
    public static void main(string []args) 
    { 
        // let us create linked list 1->2->3->4 
        linkedlist llist = new linkedlist(); 
        llist.push(4); 
        llist.push(3); 
        llist.push(2); 
        llist.push(1); 
        llist.printreverse(llist.head); 
    } 
} 
// this code has been contributed by rajput-ji  

输出

4 3 2 1

时间复杂度o(n)

如果发现不正确的内容,或者想分享有关上述主题的更多信息,请写评论。