-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo6PrintListNode.java
84 lines (71 loc) · 1.61 KB
/
No6PrintListNode.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
package com.wzx.sword;
import com.wzx.entity.ListNode;
import java.util.*;
/**
* @author wzx
* @see <a href="https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/">https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/</a>
*/
public class No6PrintListNode {
/**
* 放入栈中
* <p>
* time: O(n)
* space: O(n)
*/
public int[] reversePrint1(ListNode head) {
Deque<Integer> stack = new LinkedList<>();
while (head != null) {
stack.push(head.val);
head = head.next;
}
int[] res = new int[stack.size()];
int index = 0;
while (stack.peek() != null) {
res[index++] = stack.pop();
}
return res;
}
/**
* 反转链表
* <p>
* time: O(n)
* space: O(n)
*/
public int[] reversePrint2(ListNode head) {
if (null == head) return new int[0];
int size = 0;
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
size++;
}
int[] res = new int[size];
ListNode node = pre;
int index = 0;
while (node != null) {
res[index++] = node.val;
node = node.next;
}
return res;
}
/**
* 递归
* <p>
* time: O(n)
* space: O(n)
*/
public int[] reversePrint3(ListNode head) {
List<Integer> res = new ArrayList<>();
recursion(head, res);
return res.stream().mapToInt(i -> i).toArray();
}
private void recursion(ListNode node, List<Integer> list) {
if (node == null) return;
recursion(node.next, list);
list.add(node.val);
}
}