Skip to content

Commit 380f4f4

Browse files
committed
四刷206
1 parent eb567bd commit 380f4f4

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

docs/0206-reverse-linked-list.adoc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ image:images/0206-01.jpg[{image_attr}]
3131

3232
*提示:*
3333

34-
* 链表中节点的数目范围是 `+[0, 5000]+`
34+
* 链表中节点的数目范围是 `[0, 5000]`
3535
* `+-5000 <= Node.val <= 5000+`
3636
3737
@@ -69,6 +69,23 @@ include::{sourcedir}/_0206_ReverseLinkedList_2.java[tag=answer]
6969
----
7070
include::{sourcedir}/_0206_ReverseLinkedList_3.java[tag=answer]
7171
----
72+
73+
四刷(递归)::
74+
+
75+
--
76+
[{java_src_attr}]
77+
----
78+
include::{sourcedir}/_0206_ReverseLinkedList_41.java[tag=answer]
79+
----
80+
--
81+
82+
四刷(迭代)::
83+
+
84+
--
85+
[{java_src_attr}]
86+
----
87+
include::{sourcedir}/_0206_ReverseLinkedList_42.java[tag=answer]
88+
----
7289
--
7390
====
7491

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ endif::[]
110110
|{doc_base_url}/0146-lru-cache.adoc[题解]
111111
|✅ 链表前后指针操作
112112

113+
|{counter:codes2503}
114+
|{leetcode_base_url}/reverse-linked-list/[206. 反转链表^]
115+
|{doc_base_url}/0206-reverse-linked-list.adoc[题解]
116+
|✅ 两种解法:递归+迭代
117+
113118
|===
114119

115120
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0206_ReverseLinkedList_3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public ListNode reverseList(ListNode head) {
1414

1515
private ListNode reverseList(ListNode head, ListNode pre) {
1616
if (head == null) {
17-
return head;
17+
return pre;
1818
}
1919
ListNode result = reverseList(head.next, head);
2020
head.next = pre;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0206_ReverseLinkedList_41 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-02 15:23:04
10+
*/
11+
public ListNode reverseList(ListNode head) {
12+
return reverseList(head, null);
13+
}
14+
15+
private ListNode reverseList(ListNode head, ListNode pre) {
16+
if (head == null) {
17+
return pre;
18+
}
19+
ListNode next = head.next;
20+
ListNode result = reverseList(next, head);
21+
head.next = pre;
22+
return result;
23+
}
24+
// end::answer[]
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0206_ReverseLinkedList_42 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-02 15:39:16
10+
*/
11+
public ListNode reverseList(ListNode head) {
12+
ListNode prev = null;
13+
while (head != null) {
14+
ListNode next = head.next;
15+
head.next = prev;
16+
prev = head;
17+
head = next;
18+
}
19+
return prev;
20+
}
21+
// end::answer[]
22+
}

0 commit comments

Comments
 (0)