[jobdu]从尾到头打印链表

九度确实烂啊,用cin就超时,必须要scanf。唯一可说的就是pplast和递归打印。也可以用stack,其实和递归一样的空间复杂度。

#include<stdio.h>
using namespace std;
  
struct Node
{
    int val;
    Node * next;
public:
    Node(int _val)
    {
        val = _val;
        next = NULL;
    }
};
  
void reversePrint(Node * head)
{
    if (head == NULL) return;
    else
    {
        reversePrint(head->next);
        printf("%d
", head->val);
    }
}
  
int main()
{
    int n;
    Node * head = NULL;
    Node ** pplast = &head;
  
    while(scanf("%d",&n),n>0) {
        *pplast = new Node(n); 
        pplast = &((*pplast)->next);  
    }
    Node *node = head;
    reversePrint(head);
}

  

原文地址:https://www.cnblogs.com/lautsie/p/3267824.html