Skip to content

Commit bc5df23

Browse files
authored
feat: add solutions to lc problem: No.0206 (#4264)
1 parent 5654218 commit bc5df23

File tree

6 files changed

+119
-31
lines changed

6 files changed

+119
-31
lines changed

solution/0200-0299/0206.Reverse Linked List/README.md

+37-10
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ tags:
6767

6868
### 方法一:头插法
6969

70-
创建虚拟头节点 $dummy$,遍历链表,将每个节点依次插入 $dummy$ 的下一个节点。遍历结束,返回 $dummy.next$。
70+
我们创建一个虚拟头节点 $\textit{dummy}$,然后遍历链表,将每个节点依次插入 $\textit{dummy}$ 的下一个节点。遍历结束,返回 $\textit{dummy.next}$。
7171

72-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度
72+
时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。
7373

7474
<!-- tabs:start -->
7575

@@ -279,15 +279,15 @@ var reverseList = function (head) {
279279
*/
280280
public class Solution {
281281
public ListNode ReverseList(ListNode head) {
282-
ListNode pre = null;
283-
for (ListNode p = head; p != null;)
284-
{
285-
ListNode t = p.next;
286-
p.next = pre;
287-
pre = p;
288-
p = t;
282+
ListNode dummy = new ListNode();
283+
ListNode curr = head;
284+
while (curr != null) {
285+
ListNode next = curr.next;
286+
curr.next = dummy.next;
287+
dummy.next = curr;
288+
curr = next;
289289
}
290-
return pre;
290+
return dummy.next;
291291
}
292292
}
293293
```
@@ -466,6 +466,33 @@ impl Solution {
466466
}
467467
```
468468

469+
#### C#
470+
471+
```cs
472+
/**
473+
* Definition for singly-linked list.
474+
* public class ListNode {
475+
* public int val;
476+
* public ListNode next;
477+
* public ListNode(int val=0, ListNode next=null) {
478+
* this.val = val;
479+
* this.next = next;
480+
* }
481+
* }
482+
*/
483+
public class Solution {
484+
public ListNode ReverseList(ListNode head) {
485+
if (head == null || head.next == null) {
486+
return head;
487+
}
488+
ListNode ans = ReverseList(head.next);
489+
head.next.next = head;
490+
head.next = null;
491+
return ans;
492+
}
493+
}
494+
```
495+
469496
<!-- tabs:end -->
470497

471498
<!-- solution:end -->

solution/0200-0299/0206.Reverse Linked List/README_EN.md

+45-10
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ tags:
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Head Insertion Method
62+
63+
We create a dummy node $\textit{dummy}$, then traverse the linked list and insert each node after the $\textit{dummy}$ node. After traversal, return $\textit{dummy.next}$.
64+
65+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
6266

6367
<!-- tabs:start -->
6468

@@ -268,15 +272,15 @@ var reverseList = function (head) {
268272
*/
269273
public class Solution {
270274
public ListNode ReverseList(ListNode head) {
271-
ListNode pre = null;
272-
for (ListNode p = head; p != null;)
273-
{
274-
ListNode t = p.next;
275-
p.next = pre;
276-
pre = p;
277-
p = t;
275+
ListNode dummy = new ListNode();
276+
ListNode curr = head;
277+
while (curr != null) {
278+
ListNode next = curr.next;
279+
curr.next = dummy.next;
280+
dummy.next = curr;
281+
curr = next;
278282
}
279-
return pre;
283+
return dummy.next;
280284
}
281285
}
282286
```
@@ -287,7 +291,11 @@ public class Solution {
287291

288292
<!-- solution:start -->
289293

290-
### Solution 2
294+
### Solution 2: Recursion
295+
296+
We recursively reverse all nodes from the second node to the end of the list, then attach the $head$ to the end of the reversed list.
297+
298+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the linked list.
291299

292300
<!-- tabs:start -->
293301

@@ -451,6 +459,33 @@ impl Solution {
451459
}
452460
```
453461

462+
#### C#
463+
464+
```cs
465+
/**
466+
* Definition for singly-linked list.
467+
* public class ListNode {
468+
* public int val;
469+
* public ListNode next;
470+
* public ListNode(int val=0, ListNode next=null) {
471+
* this.val = val;
472+
* this.next = next;
473+
* }
474+
* }
475+
*/
476+
public class Solution {
477+
public ListNode ReverseList(ListNode head) {
478+
if (head == null || head.next == null) {
479+
return head;
480+
}
481+
ListNode ans = ReverseList(head.next);
482+
head.next.next = head;
483+
head.next = null;
484+
return ans;
485+
}
486+
}
487+
```
488+
454489
<!-- tabs:end -->
455490

456491
<!-- solution:end -->

solution/0200-0299/0206.Reverse Linked List/Solution.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*/
1212
public class Solution {
1313
public ListNode ReverseList(ListNode head) {
14-
ListNode pre = null;
15-
for (ListNode p = head; p != null;)
16-
{
17-
ListNode t = p.next;
18-
p.next = pre;
19-
pre = p;
20-
p = t;
14+
ListNode dummy = new ListNode();
15+
ListNode curr = head;
16+
while (curr != null) {
17+
ListNode next = curr.next;
18+
curr.next = dummy.next;
19+
dummy.next = curr;
20+
curr = next;
2121
}
22-
return pre;
22+
return dummy.next;
2323
}
24-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode ReverseList(ListNode head) {
14+
if (head == null || head.next == null) {
15+
return head;
16+
}
17+
ListNode ans = ReverseList(head.next);
18+
head.next.next = head;
19+
head.next = null;
20+
return ans;
21+
}
22+
}

solution/0200-0299/0234.Palindrome Linked List/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ tags:
6060

6161
我们可以先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。
6262

63-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度
63+
时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。
6464

6565
<!-- tabs:start -->
6666

solution/0200-0299/0234.Palindrome Linked List/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ tags:
5353

5454
<!-- solution:start -->
5555

56-
### Solution 1
56+
### Solution 1: Fast and Slow Pointers
57+
58+
We can use fast and slow pointers to find the middle of the linked list, then reverse the right half of the list. After that, we traverse both halves simultaneously, checking if the corresponding node values are equal. If any pair of values is unequal, it's not a palindrome linked list; otherwise, it is a palindrome linked list.
59+
60+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
5761

5862
<!-- tabs:start -->
5963

0 commit comments

Comments
 (0)