zoukankan      html  css  js  c++  java
  • 面试题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]
  • 相关阅读:
    java执行cmd程序
    修改linux 默认ssh的22端口
    docker入门(一)
    关键字:心跳网络、oracle rac 网络异常宕机、packet reassembles failed 、UDP error 转载大佬的一篇文章
    记录数据库心跳网络异常,导致数据库宕机--推荐使用OSWBB监控主机
    python项目一键导入所有安装包
    weblogic 中间件利用bsu 漏洞补丁升级
    weblogic 控制台密码输入5次错误被锁 / 密码忘记 修改密码的方法
    oracle数据泵工作学习记录
    简单粗暴的给Linux系统配置本地yum 源
  • 原文地址:https://www.cnblogs.com/lightmare/p/10425129.html
Copyright © 2011-2022 走看看