zoukankan      html  css  js  c++  java
  • 从尾到头打印链表

    题目描述

    输入一个链表,从尾到头打印链表每个节点的值。 
    输入描述:
    输入为链表的表头


    输出描述:
    输出为需要打印的“新链表”的表头

     1 /**
     2 *  struct ListNode {
     3 *        int val;
     4 *        struct ListNode *next;
     5 *        ListNode(int x) :
     6 *              val(x), next(NULL) {
     7 *        }
     8 *  };
     9 */
    10 class Solution {
    11 public:
    12     vector<int> printListFromTailToHead(struct ListNode* head) {
    13              std::stack<ListNode*> nodes;
    14                ListNode *pNode = head;
    15                 while(pNode != NULL)
    16                 {
    17                 nodes.push(pNode);
    18                    pNode = pNode->next;
    19                 }
    20         vector<int> result;
    21             while(!nodes.empty())
    22             {
    23                 pNode = nodes.top();
    24                    result.push_back(pNode->val);
    25                     nodes.pop();
    26             }
    27         return result;
    28     }
    29 };
  • 相关阅读:
    js基础四
    序列化和反序列化
    数组
    枚举
    Class对象和反射
    字符串String
    对象的克隆
    异常处理机制
    多继承和代码块
    接口和抽象类
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5142527.html
Copyright © 2011-2022 走看看