File tree Expand file tree Collapse file tree 2 files changed +25
-6
lines changed Expand file tree Collapse file tree 2 files changed +25
-6
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,25 @@ class Solution {
164
164
}
165
165
```
166
166
167
+ ``` java
168
+ // 从后向前递归
169
+ class Solution {
170
+ ListNode reverseList (ListNode head ) {
171
+ // 边缘条件判断
172
+ if (head == null ) return null ;
173
+ if (head. next == null ) return head;
174
+
175
+ // 递归调用,翻转第二个节点开始往后的链表
176
+ ListNode last = reverseList(head. next);
177
+ // 翻转头节点与第二个节点的指向
178
+ head. next. next = head;
179
+ // 此时的 head 节点为尾节点,next 需要指向 NULL
180
+ head. next = null ;
181
+ return last;
182
+ }
183
+ }
184
+ ```
185
+
167
186
Python迭代法:
168
187
``` python
169
188
# 双指针
Original file line number Diff line number Diff line change @@ -282,12 +282,12 @@ Java:
282
282
``` Java
283
283
// 单链表
284
284
class ListNode {
285
- int val;
286
- ListNode next;
287
- ListNode (){}
288
- ListNode (int val ) {
289
- this . val= val;
290
- }
285
+ int val;
286
+ ListNode next;
287
+ ListNode (){}
288
+ ListNode (int val ) {
289
+ this . val= val;
290
+ }
291
291
}
292
292
class MyLinkedList {
293
293
// size存储链表元素的个数
You can’t perform that action at this time.
0 commit comments