• 面试题6:从尾到头打印链表


    输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

    1.基于循环的栈

    提交时间:2018-07-11 语言:C++ 运行时间: 5 ms 占用内存:476K 状态:答案正确

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
          vector<int> printListFromTailToHead(ListNode* head) {
              vector<int> returnint;
              std::stack<ListNode* > nodes;
              ListNode* Node=head;
              while (Node!=nullptr)
              {
                  nodes.push(Node);
                  Node=Node->next;
              }
              while(!nodes.empty())
              {
                  Node=nodes.top();
                  returnint.push_back(Node->val);
                  nodes.pop();
              }
              return returnint;
        }
    };

    2.反向迭代器 .rbegin() .rend()

    提交时间:2018-07-11 语言:C++ 运行时间: 3 ms 占用内存:484K 状态:答案正确

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
            vector<int> printListFromTailToHead(ListNode* head) {
            vector<int> v;                         
            ListNode *p = head;
            while (p != nullptr) {
               v.push_back(p->val);
               p = p->next;
            }
            //反向迭代器创建临时对象
            return vector<int>(v.rbegin(), v.rend());
        }
    };

    3.递归

    提交时间:2018-07-11 语言:C++ 运行时间: 3 ms 占用内存:480K 状态:答案正确

    /**
    *  struct ListNode {
    *        int val;
    *        struct ListNode *next;
    *        ListNode(int x) :
    *              val(x), next(NULL) {
    *        }
    *  };
    */
    class Solution {
    public:
         vector<int> dev;
         vector<int>& printListFromTailToHead(struct ListNode* head) {
             if(head!=NULL) {
                 if(head->next!=NULL) {
                    dev=printListFromTailToHead(head->next);
                  }
                  dev.push_back(head->val);
             }
             return dev;
         }
    };

    JAVA

    1.

    提交时间:2018-07-11 语言:Java 运行时间: 20 ms 占用内存:9248K 状态:答案正确

    /**
    *    public class ListNode {
    *        int val;
    *        ListNode next = null;
    *
    *        ListNode(int val) {
    *            this.val = val;
    *        }
    *    }
    *
    */
    import java.util.ArrayList;
    public class Solution {
            public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
            ArrayList<Integer> list=new ArrayList<Integer>();
            ListNode pre=null;
            ListNode next=null;
            while(listNode!=null){
                next=listNode.next;
                listNode.next=pre;
                pre=listNode;
                listNode=next;
            }
            while(pre!=null){
                list.add(pre.val);
                pre=pre.next;
            }
            return list;
        }
    }

    2.

    提交时间:2018-07-11 语言:Java 运行时间: 20 ms 占用内存:9328K 状态:答案正确

    /**
    *    public class ListNode {
    *        int val;
    *        ListNode next = null;
    *
    *        ListNode(int val) {
    *            this.val = val;
    *        }
    *    }
    *
    */
    import java.util.ArrayList;
    public class Solution {
         ArrayList<Integer> arrayList=new ArrayList<Integer>();
        public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
            if(listNode!=null){
                this.printListFromTailToHead(listNode.next);
                arrayList.add(listNode.val);
            }
            return arrayList;
        }
        }

    Python递归

    提交时间:2018-07-11 语言:Python 运行时间: 27 ms 占用内存:5728K 状态:答案正确

    # -*- coding:utf-8 -*-
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
     
    class Solution:
        # 返回从尾部到头部的列表值序列,例如[1,2,3]
        def printListFromTailToHead(self, listNode):
            # write code here
            if listNode is None:
                return []
            return self.printListFromTailToHead(listNode.next) + [listNode.val]
  • 相关阅读:
    FZU-2087 统计树边(最小生成树)
    HDU-1599 find the mincost route(floyd求最小环)
    BZOJ-1191 [HNOI2006]超级英雄Hero(二分图匹配)
    FZU-2020 组合(Lucas定理)
    FZU-2232 炉石传说(二分图匹配)
    NOIP2016模拟 拼接mf(模拟)
    2016年11月10日00:26:08
    BZOJ2986 Non-Squarefree Numbers
    BZOJ3624 [Apio2008]免费道路
    BZOJ3224 Tyvj 1728 普通平衡树
  • 原文地址:https://www.cnblogs.com/lightmare/p/10425129.html
Copyright © 2020-2023  润新知