zoukankan      html  css  js  c++  java
  • [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);
    }
    

      

  • 相关阅读:
    WPF笔记(1)
    Java笔记(3)
    Java笔记(2)
    Java笔记(1)
    PHP学习笔记(5)
    PHP学习笔记(4)
    PHP学习笔记(3)
    PHP学习笔记(2)
    PHP学习笔记(1)
    WKWebView 支持https请求
  • 原文地址:https://www.cnblogs.com/lautsie/p/3267824.html
Copyright © 2011-2022 走看看