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

```class Solution {
public:
std::stack<ListNode*>nodes;
vector<int> ret;
while(pNode != NULL){
nodes.push(pNode);
pNode = pNode->next;
}
while(!nodes.empty()){
ret.push_back(nodes.top()->val);
nodes.pop();
}
return ret;
}
};
```

```/**
*  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;
}
};
```

```有三种思路，第一就是利用栈先入后出的特性完成，第二就是存下来然后进行数组翻转。

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;
}
};

```

```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 []

```/*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;
}
```

/**
*    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;
}
};```

```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;
}
}

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

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;
}

};```

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

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
# 返回从尾部到头部的列表值序列，例如[1,2,3]
# 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
```

```vector<int> printListFromTailToHead(struct ListNode* head) {
vector<int> v;
{
}
return v;
}```

```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);
}
}```

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

```public class LinkedList {
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;
public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
while(listNode != null) {
listNode = listNode.next;
}
return list;
}
}
```

```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;

}
}
```

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

2220条回答 360172浏览

# 通过挑战的用户

• 2019-10-21 12:48:16
• 2019-10-21 12:34:22
• 2019-10-21 12:22:34
• 2019-10-21 12:19:13
• 2019-10-21 11:47:01

# 相关试题

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

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