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 };
  • 相关阅读:
    Lambda
    Thread&线程池
    异常
    Map
    List and Set
    Collection和迭代器Iterator
    Object类,常用API
    (一)自定义 mybatis 之框架介绍
    Nginx三大功能及高并发分流
    http协议改为https
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5142527.html
Copyright © 2011-2022 走看看