从尾到头打印链表

输入一个链表,从尾到头打印链表每个节点的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* 运行时间:26ms
* 占用内存:9320k
* @param listNode
* @return
*/
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

ArrayList<Integer> list = new ArrayList<Integer>();
ListNode pNode = listNode;

if (pNode != null) {
if (listNode.next != null) {
list.addAll(printListFromTailToHead(listNode.next));
}
list.add(listNode.val);
}

return list;
}

/**
* 运行时间:20ms
* 占用内存:8708k
* @param listNode
* @return
*/
public ArrayList<Integer> printListFromTailToHead2(ListNode listNode) {
ArrayList<Integer> revLists = new ArrayList<Integer>();
Stack<Integer> stack = new Stack<Integer>();

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

while (!stack.empty()) {
revLists.add(stack.pop());
}

return revLists;
}

/**
* 运行时间:30ms
* 占用内存:9296k
* @param listNode
* @return
*/
public ArrayList<Integer> printListFromTailToHead1(ListNode listNode) {
ArrayList<Integer> lists = new ArrayList<Integer>();
ArrayList<Integer> revLists = new ArrayList<Integer>();

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

for (int i = lists.size() - 1; i >= 0; i--) {
System.out.println(lists.get(i));
revLists.add(lists.get(i));
}

return revLists;
}
1
2
3
4
5
6
7
8
class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
this.val = val;
}
}