Skip to content

Commit 2d742c2

Browse files
committed
二刷206
1 parent 55d7cf7 commit 2d742c2

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

docs/0206-reverse-linked-list.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,39 @@ Reverse a singly linked list.
2020

2121
A linked list can be reversed either iteratively or recursively. Could you implement both?
2222

23+
== 思路分析
2324

25+
image::images/0206-01.png[{image_attr}]
2426

2527
[[src-0206]]
28+
[tabs]
29+
====
30+
一刷::
31+
+
32+
--
2633
[{java_src_attr}]
2734
----
2835
include::{sourcedir}/_0206_ReverseLinkedList.java[tag=answer]
2936
----
37+
--
3038
39+
二刷::
40+
+
41+
--
42+
[{java_src_attr}]
43+
----
44+
include::{sourcedir}/_0206_ReverseLinkedList_2.java[tag=answer]
45+
----
46+
--
47+
====
48+
49+
== 思考题
50+
51+
可以尝试一下递归!
52+
53+
另外,思考一下如何通过传参来简化代码。
54+
55+
== 参考资料
56+
57+
. https://leetcode.cn/problems/reverse-linked-list/solutions/551596/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/?envType=study-plan-v2&envId=selected-coding-interview[206. 反转链表 - 官方题解^]
58+
. https://leetcode.cn/problems/reverse-linked-list/solutions/2361282/206-fan-zhuan-lian-biao-shuang-zhi-zhen-r1jel/?envType=study-plan-v2&envId=selected-coding-interview[206. 反转链表 - 双指针,清晰图解^]

docs/images/0206-01.png

19 KB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@
469469
|{doc_base_url}/0343-integer-break.adoc[题解]
470470
|动态规划
471471

472+
|{counter:codes}
473+
|{leetcode_base_url}/reverse-linked-list/[206. Reverse Linked List^]
474+
|{doc_base_url}/0206-reverse-linked-list.adoc[题解]
475+
|递给解法非常妙!
476+
472477
|===
473478

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

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ public class _0206_ReverseLinkedList {
3636
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List.
3737
*
3838
* Memory Usage: 38 MB, less than 6.48% of Java online submissions for Reverse Linked List.
39+
*
40+
* @author D瓜哥 · https://www.diguage.com
41+
* @since 2020-01-12 12:47
3942
*/
4043
public ListNode reverseList(ListNode head) {
41-
if (Objects.isNull(head)) {
42-
return null;
43-
}
44-
ListNode result = null;
45-
ListNode current = head;
46-
while (Objects.nonNull(current)) {
47-
ListNode pre = current.next;
48-
current.next = result;
49-
result = current;
50-
current = pre;
51-
}
52-
return result;
44+
if (head == null || head.next == null) {
45+
return head;
46+
}
47+
ListNode curr = null;
48+
while (head != null) {
49+
ListNode next = head.next;
50+
head.next = curr;
51+
curr = head;
52+
head = next;
53+
}
54+
return curr;
5355
}
5456

5557
/**
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.ListNode;
4+
5+
public class _0206_ReverseLinkedList_2 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2024-09-13 16:24:00
10+
*/
11+
public ListNode reverseList(ListNode head) {
12+
return reverseList(head, null);
13+
}
14+
15+
public ListNode reverseList(ListNode head, ListNode pre) {
16+
if (head == null) {
17+
return pre;
18+
}
19+
ListNode result = reverseList(head.next, head);
20+
head.next = pre;
21+
return result;
22+
}
23+
// end::answer[]
24+
}

0 commit comments

Comments
 (0)