-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintListFromTailToHead.java
More file actions
41 lines (40 loc) · 1.15 KB
/
printListFromTailToHead.java
File metadata and controls
41 lines (40 loc) · 1.15 KB
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
//利用栈的性质,先把list中的值依次读入栈中,然后在把栈中的元素pop出来
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list=new ArrayList<Integer>();
Stack<Integer> stk = new Stack<Integer>();
while(listNode!=null)
{
stk.push(listNode.val);
listNode=listNode.next;
}
whle(!stk.isEmpty())
{
list.add(stk.pop());
}
/* int top=stk.size()-1;//栈顶的指针为maxsize-1;
while(top!=-1)
{
// int temp=stk.pop();
list.add(stk.pop());
top--;
}
*/
return list;
}
}
//利用迭代的方式进行打印
import java.util.ArrayList;
public class Solution {
ArrayList<Integer> list=new ArrayList<Integer>();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode!=null)
{
this.printListFromTailToHead(listNode.next);
list.add(listNode.val);
}
return list;
}
}