-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_6ReversePrint.java
More file actions
48 lines (40 loc) · 948 Bytes
/
Copy path_6ReversePrint.java
File metadata and controls
48 lines (40 loc) · 948 Bytes
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
package offer;
import java.util.Stack;
/**
* @author : CodeWater
* @create :2022-03-11-17:38
* @Function Description :
* <p>
* 剑指 Offer 06. 从尾到头打印链表
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class _6ReversePrint {
public int[] reversePrint(ListNode head) {
//用栈!!!!
Stack<ListNode> stack = new Stack<ListNode>();
ListNode temp = head;
while (temp != null) {
stack.push(temp);
//java里面是链表是用点指向下一个
temp = temp.next;
}
int size = stack.size();
int[] a = new int[size];
for (int i = 0; i < size; i++) {
a[i] = stack.pop().val;
}
return a;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}