-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSolution.java
98 lines (85 loc) · 2.85 KB
/
Solution.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package ds.link.targetoffer06;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/**
* 从尾到头打印链表
* 剑指 Offer 06 https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
* 方法1: 先把链表反转,然后在遍历输出。 但是因为反转了一遍,所以会改变链表的结构,并不是每一个场景都允许。
* 方法2: 正常遍历链表,用一个栈收集链表中的元素,然后遍历输出该栈。
* 方法3: 递归输出链表的下一个节点,然后再输出当前节点。
*
* @author yangyi 2021年01月17日19:20:44
*/
public class Solution {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
private List<Integer> result = new LinkedList<>();
/**
* 递归实现
*/
public int[] reversePrint(ListNode head) {
recursionLink(head);
int[] res = new int[result.size()];
for (int i = 0; i < result.size(); i++) {
res[i] = result.get(i);
}
return res;
}
private void recursionLink(ListNode head) {
if (head != null) {
if (head.next != null) {
recursionLink(head.next);
}
result.add(head.val);
}
}
private ListNode createLink() {
ListNode node_1 = new ListNode(1);
ListNode node_3 = new ListNode(3);
ListNode node_2 = new ListNode(2);
node_1.next = node_3;
node_3.next = node_2;
return node_1;
}
private void printLink(ListNode head) {
for (ListNode cur = head; cur != null; cur = cur.next) {
System.out.print(cur.val + " ");
}
}
/**
* 借助栈实现
*/
public int[] reversePrintLink(ListNode head) {
if (head == null) {
return new int[]{};
}
Stack<ListNode> nodeStack = new Stack<>();
for (ListNode node = head; node != null; node = node.next) {
nodeStack.push(node);
}
int size = nodeStack.size();
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = nodeStack.pop().val;
}
return result;
}
public static void main(String[] args) {
Solution reversePrintLink = new Solution();
System.out.println("先构造一个测试用的链表:");
ListNode head = reversePrintLink.createLink();
reversePrintLink.printLink(head);
System.out.println();
System.out.println("采用栈逆序输出:");
System.out.println(Arrays.toString(reversePrintLink.reversePrintLink(head)));
System.out.println("采用递归逆序输出:");
System.out.println(Arrays.toString(reversePrintLink.reversePrint(reversePrintLink.createLink())));
}
}