面试题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]
posted @ 2018-07-12 00:14  lightmare  阅读(143)  评论(0编辑  收藏  举报