Skip to content

Commit 52597c1

Browse files
committed
feat: add solutions to lcof problem: No.06
1 parent 8a8610f commit 52597c1

File tree

3 files changed

+136
-91
lines changed

3 files changed

+136
-91
lines changed

lcof/面试题06. 从尾到头打印链表/README.md

Lines changed: 122 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@
1919

2020
## 解法
2121

22-
该题需要将链表转换为数组,且需要反向。由于目标是链表,无法第一时间得知长度,声明等长数组。
22+
**方法一:顺序遍历 + 反转**
2323

24-
解题方案:
24+
我们可以顺序遍历链表,将每个节点的值存入数组中,然后将数组反转。
2525

26-
- 遍历
27-
- 从头到尾遍链表,获取链表长度,声明等长数组;
28-
- 再次遍历并放入数组当中,在数组中的放置顺序是从尾到头。
29-
- 递归
30-
- 记录深度,递归到链表尾部;
31-
- 将深度化为数组长度,将回溯结果正序放入数组当中。
32-
- 动态数组
33-
- 遍历链表,将元素放入数组当中;
34-
- 遍历结束,将数组倒置后返回(`reverse()`)。
26+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
27+
28+
**方法二:递归**
29+
30+
我们可以使用递归的方式,先递归得到 `head` 之后的节点反过来的值列表,然后将 `head` 的值加到列表的末尾。
31+
32+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的长度。
3533

3634
<!-- tabs:start -->
3735

@@ -54,9 +52,23 @@ class Solution:
5452
return ans[::-1]
5553
```
5654

57-
### **Java**
55+
```python
56+
# Definition for singly-linked list.
57+
# class ListNode:
58+
# def __init__(self, x):
59+
# self.val = x
60+
# self.next = None
61+
62+
class Solution:
63+
def reversePrint(self, head: ListNode) -> List[int]:
64+
if head is None:
65+
return []
66+
ans = self.reversePrint(head.next)
67+
ans.append(head.val)
68+
return ans
69+
```
5870

59-
栈实现:
71+
### **Java**
6072

6173
```java
6274
/**
@@ -83,8 +95,6 @@ class Solution {
8395
}
8496
```
8597

86-
先计算链表长度 n,然后创建一个长度为 n 的结果数组。最后遍历链表,依次将节点值存放在数组上(从后往前):
87-
8898
```java
8999
/**
90100
* Definition for singly-linked list.
@@ -96,41 +106,62 @@ class Solution {
96106
*/
97107
class Solution {
98108
public int[] reversePrint(ListNode head) {
99-
if (head == null) {
100-
return new int[] {};
101-
}
102109
int n = 0;
103-
for (ListNode cur = head; cur != null; cur = cur.next, ++n)
104-
;
110+
ListNode cur = head;
111+
for (; cur != null; cur = cur.next) {
112+
++n;
113+
}
105114
int[] ans = new int[n];
106-
for (ListNode cur = head; cur != null; cur = cur.next) {
115+
cur = head;
116+
for (; cur != null; cur = cur.next) {
107117
ans[--n] = cur.val;
108118
}
109119
return ans;
110120
}
111121
}
112122
```
113123

114-
### **JavaScript**
124+
### **C++**
115125

116-
```js
126+
```cpp
117127
/**
118128
* Definition for singly-linked list.
119-
* function ListNode(val) {
120-
* this.val = val;
121-
* this.next = null;
122-
* }
129+
* struct ListNode {
130+
* int val;
131+
* ListNode *next;
132+
* ListNode(int x) : val(x), next(NULL) {}
133+
* };
123134
*/
135+
class Solution {
136+
public:
137+
vector<int> reversePrint(ListNode* head) {
138+
if (!head) return {};
139+
vector<int> ans = reversePrint(head->next);
140+
ans.push_back(head->val);
141+
return ans;
142+
}
143+
};
144+
```
145+
146+
```cpp
124147
/**
125-
* @param {ListNode} head
126-
* @return {number[]}
148+
* Definition for singly-linked list.
149+
* struct ListNode {
150+
* int val;
151+
* ListNode *next;
152+
* ListNode(int x) : val(x), next(NULL) {}
153+
* };
127154
*/
128-
var reversePrint = function (head) {
129-
let ans = [];
130-
for (; !!head; head = head.next) {
131-
ans.unshift(head.val);
155+
class Solution {
156+
public:
157+
vector<int> reversePrint(ListNode* head) {
158+
vector<int> ans;
159+
for (; head; head = head->next) {
160+
ans.push_back(head->val);
161+
}
162+
reverse(ans.begin(), ans.end());
163+
return ans;
132164
}
133-
return ans;
134165
};
135166
```
136167

@@ -144,59 +175,77 @@ var reversePrint = function (head) {
144175
* Next *ListNode
145176
* }
146177
*/
147-
func reversePrint(head *ListNode) []int {
148-
ans := []int{}
149-
for head != nil {
150-
ans = append([]int{head.Val}, ans...)
151-
head = head.Next
178+
func reversePrint(head *ListNode) (ans []int) {
179+
for ; head != nil; head = head.Next {
180+
ans = append(ans, head.Val)
152181
}
153-
return ans
182+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
183+
ans[i], ans[j] = ans[j], ans[i]
184+
}
185+
return
154186
}
155187
```
156188

157-
### **C++**
189+
```go
190+
/**
191+
* Definition for singly-linked list.
192+
* type ListNode struct {
193+
* Val int
194+
* Next *ListNode
195+
* }
196+
*/
197+
func reversePrint(head *ListNode) (ans []int) {
198+
if head == nil {
199+
return
200+
}
201+
ans = reversePrint(head.Next)
202+
ans = append(ans, head.Val)
203+
return
204+
}
205+
```
158206

159-
递归实现:
207+
### **JavaScript**
160208

161-
```cpp
209+
```js
162210
/**
163211
* Definition for singly-linked list.
164-
* struct ListNode {
165-
* int val;
166-
* ListNode *next;
167-
* ListNode(int x) : val(x), next(NULL) {}
168-
* };
212+
* function ListNode(val) {
213+
* this.val = val;
214+
* this.next = null;
215+
* }
169216
*/
170-
class Solution {
171-
public:
172-
vector<int> reversePrint(ListNode* head) {
173-
if (!head) return {};
174-
vector<int> ans = reversePrint(head->next);
175-
ans.push_back(head->val);
176-
return ans;
217+
/**
218+
* @param {ListNode} head
219+
* @return {number[]}
220+
*/
221+
var reversePrint = function (head) {
222+
let ans = [];
223+
for (; !!head; head = head.next) {
224+
ans.unshift(head.val);
177225
}
226+
return ans;
178227
};
179228
```
180229

181-
栈实现:
182-
183-
```cpp
184-
class Solution {
185-
public:
186-
vector<int> reversePrint(ListNode* head) {
187-
stack<int> stk;
188-
vector<int> ans;
189-
ListNode *p = head;
190-
while (p) {
191-
stk.push(p->val);
192-
p = p->next;
193-
}
194-
while (!stk.empty()) {
195-
ans.push_back(stk.top());
196-
stk.pop();
197-
}
198-
return ans;
230+
```js
231+
/**
232+
* Definition for singly-linked list.
233+
* function ListNode(val) {
234+
* this.val = val;
235+
* this.next = null;
236+
* }
237+
*/
238+
/**
239+
* @param {ListNode} head
240+
* @return {number[]}
241+
*/
242+
var reversePrint = function (head) {
243+
if (!head) {
244+
return [];
199245
}
246+
const ans = reversePrint(head.next);
247+
ans.push(head.val);
248+
return ans;
200249
};
201250
```
202251

@@ -226,8 +275,6 @@ function reversePrint(head: ListNode | null): number[] {
226275

227276
### **Rust**
228277

229-
动态数组:
230-
231278
```rust
232279
// Definition for singly-linked list.
233280
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -259,8 +306,6 @@ impl Solution {
259306
}
260307
```
261308

262-
遍历:
263-
264309
```rust
265310
// Definition for singly-linked list.
266311
// #[derive(PartialEq, Eq, Clone, Debug)]

lcof/面试题06. 从尾到头打印链表/Solution.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
* Next *ListNode
66
* }
77
*/
8-
func reversePrint(head *ListNode) []int {
9-
ans := []int{}
10-
for head != nil {
11-
ans = append([]int{head.Val}, ans...)
12-
head = head.Next
8+
func reversePrint(head *ListNode) (ans []int) {
9+
for ; head != nil; head = head.Next {
10+
ans = append(ans, head.Val)
1311
}
14-
return ans
12+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
13+
ans[i], ans[j] = ans[j], ans[i]
14+
}
15+
return
1516
}

lcof/面试题06. 从尾到头打印链表/Solution.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
*/
99
class Solution {
1010
public int[] reversePrint(ListNode head) {
11-
if (head == null) {
12-
return new int[] {};
11+
Deque<Integer> stk = new ArrayDeque<>();
12+
for (; head != null; head = head.next) {
13+
stk.push(head.val);
1314
}
14-
int n = 0;
15-
for (ListNode cur = head; cur != null; cur = cur.next, ++n)
16-
;
17-
int[] ans = new int[n];
18-
for (ListNode cur = head; cur != null; cur = cur.next) {
19-
ans[--n] = cur.val;
15+
int[] ans = new int[stk.size()];
16+
int i = 0;
17+
while (!stk.isEmpty()) {
18+
ans[i++] = stk.pop();
2019
}
2120
return ans;
2221
}

0 commit comments

Comments
 (0)