File tree Expand file tree Collapse file tree 5 files changed +71
-12
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 5 files changed +71
-12
lines changed Original file line number Diff line number Diff line change @@ -20,11 +20,39 @@ Reverse a singly linked list.
20
20
21
21
A linked list can be reversed either iteratively or recursively. Could you implement both?
22
22
23
+ == 思路分析
23
24
25
+ image::images/0206-01.png[{image_attr}]
24
26
25
27
[[src-0206]]
28
+ [tabs]
29
+ ====
30
+ 一刷::
31
+ +
32
+ --
26
33
[{java_src_attr}]
27
34
----
28
35
include::{sourcedir}/_0206_ReverseLinkedList.java[tag=answer]
29
36
----
37
+ --
30
38
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. 反转链表 - 双指针,清晰图解^]
Original file line number Diff line number Diff line change 469
469
|{doc_base_url} /0343-integer-break.adoc[题解]
470
470
|动态规划
471
471
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
+
472
477
|===
473
478
474
479
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change @@ -36,20 +36,22 @@ public class _0206_ReverseLinkedList {
36
36
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List.
37
37
*
38
38
* 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
39
42
*/
40
43
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 ;
53
55
}
54
56
55
57
/**
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments