Skip to content

Commit 62d523d

Browse files
Merge pull request youngyangyang04#858 from hailincai/master
Update 设计链表和翻转链表
2 parents 9cfb604 + 7f8630a commit 62d523d

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

problems/0206.翻转链表.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,25 @@ class Solution {
164164
}
165165
```
166166

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+
167186
Python迭代法:
168187
```python
169188
#双指针

problems/0707.设计链表.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,12 @@ Java:
282282
```Java
283283
//单链表
284284
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+
}
291291
}
292292
class MyLinkedList {
293293
//size存储链表元素的个数

0 commit comments

Comments
 (0)