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

    题目描述

    输入一个链表,从尾到头打印链表每个节点的值。
    输入描述:
    输入为链表的表头
    输出描述:
    输出为需要打印的“新链表”的表头

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
        vector<int> printListFromTailToHead(struct ListNode* head) {
            std::stack<ListNode*> nodes;
            vector<int> result;
            ListNode* pNode = head;
            while(pNode != NULL){
                nodes.push(pNode);
                pNode = pNode->next;
            }
            
            while(!nodes.empty()){
                pNode = nodes.top();
                result.push_back(pNode->val);
                nodes.pop();
            }
            return result;
        }
    };
    转载请注明出处: C++博客园:godfrey_88 http://www.cnblogs.com/gaobaoru-articles/
  • 相关阅读:
    原型
    构造函数
    异常处理
    逻辑中断
    1. 两数之和
    面向对象(进阶篇)
    面向对象(初级篇)
    面向对象
    迭代器/生成器
    模块&字符格式化
  • 原文地址:https://www.cnblogs.com/gaobaoru-articles/p/5235269.html
Copyright © 2011-2022 走看看