输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。
如输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]
0 <= 链表长度 <= 10000
java 递归超简洁版本 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的实现这么少, 我来添砖加瓦 # 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
# -*- coding:utf-8-*-# classListNode:# def __init__(self, x):# self.val = x# self.next = NoneclassSolution:# 返回从尾部到头部的列表值序列,例如[1,2,3]def printListFromTailToHead(self, listNode):# write code hereresult = []iflistNode is None:returnresultwhilelistNode.next is not None:result.extend([listNode.val])listNode=listNode.nextresult.extend([listNode.val])returnresult[::-1]
import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { 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){ list.add(listNode.val); return; } get(listNode.next, list); list.add(listNode.val); } }
}
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){ result.add(top.val); top = top.next; } result.remove(result.size()-1); return result; } }
/** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.*; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { //用来存储链表中节点的值。 Stack<Integer> reverse = new Stack<>(); while(listNode != null){ reverse.push(listNode.val); listNode = listNode.next; } //创建的题目要求的数据类型来存储反向的节点值。 ArrayList<Integer> result = new ArrayList<>(); while(!reverse.isEmpty()){ //将值从栈中弹出,并添加到ArrayList中 result.add(reverse.pop()); } return result; } }
import java.util.ArrayList; import java.util.Stack; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { 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()){ list.add(new Integer(stack.pop().val)); } return list; } }
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> res = new ArrayList<>();
helper(res, listNode);
return res;
}
private void helper(ArrayList<Integer> res, ListNode head) {
if (head != null) {
if (head.next != null) {
helper(res, head.next);
}
res.add(head.val);
}
}
从尾到头,即先进后出,使用栈。注意,JDK推荐使用Deque代替Stack。
public ArrayList<Integer> printListFromTailToHead2(ListNode listNode) {
ArrayList<Integer> res = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
while (listNode != null) {
stack.push(listNode.val);
listNode = listNode.next;
}
while (!stack.isEmpty()) {
res.add(stack.pop());
}
return res;
}
public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> list=new ArrayList<Integer>(); while(listNode!=null) { list.add(0,listNode.val); listNode=listNode.next; } return list; } }定义一个集合,遍历链表,每次都放在集合的第一个,返回集合,但是这样时间复杂度就有点高了
class Solution { public: vector<int> printListFromTailToHead(ListNode* pHead) { vector<int> rst; while(pHead != nullptr) { rst.push_back(pHead->val); pHead = pHead->next; } int sz = rst.size(); for(int i = 0; i < (sz>>1); ++i) { rst[i] ^= rst[sz-1-i]; rst[sz-1-i] ^= rst[i]; rst[i] ^= rst[sz-1-i]; } return rst; } };
直接翻转容器中的数据。用到了三次异或位运算交换首位对称的元素;也可以用<algorithm>里的reverse(rst.begin(),rst.end());
就地反转 不依靠别的结构
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList list=new ArrayList();
if(listNode==null) return list;
ListNode dummy=new ListNode(0);
dummy.next=listNode;
ListNode cur=listNode;
// 链表就地反转
while(cur.next!=null)
{
ListNode temp=cur.next;
cur.next=temp.next;
temp.next=dummy.next;
dummy.next=temp;
}
ListNode head=dummy.next;
while(head!=null)
{
list.add(head.val);
head=head.next;
}
return list;
}
}
利用栈
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList list=new ArrayList();
if(listNode==null) return list;
Stack<ListNode> s=new Stack<>();
while(listNode!=null)
{
s.push(listNode);
listNode=listNode.next; }
while(!s.isEmpty())
{
list.add(s.pop().val);
}
return list;
}
}