[编程题]从尾到头打印链表

# 1980个回答

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {         if(listNode==null) {             return new ArrayList<Integer>();         }         else {             Stack<Integer> result = new Stack<Integer>();             result.push(listNode.val);             ListNode currentNode = listNode;             while(currentNode.next!=null) {                 currentNode = currentNode.next;                 result.push(currentNode.val);             }             ArrayList<Integer> buffer = new ArrayList<Integer>();             while(result.size()!=0) {                 buffer.add(result.pop());             }             return buffer;         }
}

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
vector<int> value;
{
{
if(tempVec.size()>0)
value.insert(value.begin(),tempVec.begin(),tempVec.end());
}

}
return value;
}
};

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
vector<int> value;
{
{
}

}
return value;
}
};

java 递归超简洁版本
public class Solution {
ArrayList<Integer> arrayList=new ArrayList<Integer>();
if(listNode!=null){
}
return arrayList;
}
}

python版递归法，只有3行代码

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
# 返回从尾部到头部的列表值序列，例如[1,2,3]
# write code here
if listNode is None:
return []

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
Stack<Integer> stack=new Stack<Integer>();
while(listNode!=null){
stack.push(listNode.val);
listNode=listNode.next;
}

ArrayList<Integer> list=new ArrayList<Integer>();
while(!stack.isEmpty()){
}
return list;
}
}

import java.util.ArrayList;
import java.util.Stack;
public class Solution {
ArrayList<Integer> list=new ArrayList<Integer>();

ListNode pNode=listNode;
if(pNode!=null){
if(pNode.next!=null){
}
}

return list;
}
}

C++版，递归
class Solution {
public:
vector<int> dev;
}
}
return dev;
}
};

class Solution {
public:
vector<int> value;
ListNode *p=NULL;
stack<int> stk;
while(p!=NULL){
stk.push(p->val);
p=p->next;
}
while(!stk.empty()){
value.push_back(stk.top());
stk.pop();
}
return value;
}
};

class Solution {
public:
vector<int> value;
ListNode *p=NULL;
while(p!=NULL){
value.push_back(p->val);
p=p->next;
}
//reverse(value.begin(),value.end()); //C++自带的翻转函数
int temp=0;
int i=0,j=value.size()-1;
while(i<j){
temp=value[i];    //也可以用swap函数，swap(value[i],value[j]);
value[i]=value[j];
value[j]=temp;
i++;
j--;
}
return value;
}
};

class Solution {
public:
vector<int> value;
ListNode *p=NULL;
if(p!=NULL){
if(p->next!=NULL){
}
value.push_back(p->val);
}
return value;
}
};

/*function ListNode(x) {
this.val = x;
this.next = null;
}*/
// write code here
var res = [], pNode = head;
while (pNode != null) {
res.unshift(pNode.val);
pNode = pNode.next;
}
return res;
}

import java.util.ArrayList;
import java.util.Collections;
public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();

while(listNode != null){
listNode = listNode.next;
}

Collections.reverse(list);//使用Collections的reverse方法，直接将list反转
return list;
}
}

class Solution {
public:
vector<int> v;

while (p != nullptr) {
v.push_back(p->val);
p = p->next;
}
//反向迭代器创建临时对象
return vector<int>(v.rbegin(), v.rend());
}
};

# -*- coding:utf-8-*-
# classListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

classSolution:
# 返回从尾部到头部的列表值序列，例如[1,2,3]
# write code here
result = []
iflistNode is None:
returnresult

whilelistNode.next is not None:
result.extend([listNode.val])
listNode=listNode.next
result.extend([listNode.val])

returnresult[::-1]

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.Stack;
import java.util.ArrayList;
public class Solution {
if(listNode == null){
ArrayList list = new ArrayList();
return list;
}
Stack<Integer> stk = new Stack<Integer>();
while(listNode != null){
stk.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> arr = new ArrayList<Integer>();
while(!stk.isEmpty()){
}
return arr;
}
}

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
//利用栈的逆序输出特性
stack<int> stack;
vector<int> vector;
stack.push(p->val);
while((p=p->next) != NULL) {
stack.push(p->val);
}
while(!stack.empty()) {
vector.push_back(stack.top());
stack.pop();
}
}
return vector;
}

};

import java.util.ArrayList;
public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
if(listNode==null)
return list;
get(listNode, list);
return list;
}
public void get(ListNode listNode,ArrayList<Integer> list){
if(listNode.next==null){
return;
}
get(listNode.next, list);
}
}

vector<int> v;
{
}
return v;
}

# python的实现这么少， 我来添砖加瓦
# 1.先遍历链表元素到栈
# 2.栈再弹出

# -*- 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         lst,lst_bak = [],[]         if not listNode:             return lst         while listNode:             lst.append(listNode.val)             listNode = listNode.next         while lst:             lst_bak.append(lst.pop())         return lst_bak

/*function ListNode(x){
this.val = x;        // 节点的数据域
this.next = null;    // 节点指针域，通过this.next使指针后移
}*/
{
var arr = [];    // 创建一个空数组，将每个节点存放哎数组中
return arr;
}
}

return arr;
}

ArrayList<Integer> list = new ArrayList<Integer>();
if(listNode == null)
return list;
while(listNode.next != null){
listNode = listNode.next;
}
return list;
}

public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ListNode list = new ListNode(0);

ListNode cursor = listNode;
ListNode top = list;

ArrayList<Integer> result = new ArrayList<Integer>();
while(cursor!=null){
ListNode temp = new ListNode(cursor.val);
temp.next = top;
top = temp;
cursor = cursor.next;
}

while(top!=null){
top = top.next;
}
result.remove(result.size()-1);

return result;

}

}

import java.util.ArrayList;
import java.util.Stack;
public class Solution {

Stack<ListNode> stack = new Stack<ListNode>();
ArrayList<Integer> list=new ArrayList<Integer>();
ListNode current=listNode;
while(current!=null){
stack.push(current);
current=current.next;
}
while(!stack.isEmpty()){
}

return list;

}
}

import java.util.ArrayList;
public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
while(listNode != null) {
listNode = listNode.next;
}
return list;
}
}

ArrayList<Integer> list = new ArrayList<>();
for(;listNode != null;) {
listNode = listNode.next;
}
return list;
}

1980条回答 311079浏览

# 通过挑战的用户

• 2019-06-18 19:39:48
• 2019-06-18 19:29:35
• 2019-06-18 19:28:49
• 2019-06-18 19:28:36
• 2019-06-18 19:26:56

# 相关试题

• 扫描二维码，关注牛客网

• 下载牛客APP，随时随地刷题