Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0206 #4264

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions solution/0200-0299/0206.Reverse Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ tags:

### 方法一:头插法

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -279,15 +279,15 @@ var reverseList = function (head) {
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode pre = null;
for (ListNode p = head; p != null;)
{
ListNode t = p.next;
p.next = pre;
pre = p;
p = t;
ListNode dummy = new ListNode();
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = dummy.next;
dummy.next = curr;
curr = next;
}
return pre;
return dummy.next;
}
}
```
Expand Down Expand Up @@ -466,6 +466,33 @@ impl Solution {
}
```

#### C#

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode ans = ReverseList(head.next);
head.next.next = head;
head.next = null;
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
55 changes: 45 additions & 10 deletions solution/0200-0299/0206.Reverse Linked List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Head Insertion Method

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}$.

The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -268,15 +272,15 @@ var reverseList = function (head) {
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode pre = null;
for (ListNode p = head; p != null;)
{
ListNode t = p.next;
p.next = pre;
pre = p;
p = t;
ListNode dummy = new ListNode();
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = dummy.next;
dummy.next = curr;
curr = next;
}
return pre;
return dummy.next;
}
}
```
Expand All @@ -287,7 +291,11 @@ public class Solution {

<!-- solution:start -->

### Solution 2
### Solution 2: Recursion

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.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the linked list.

<!-- tabs:start -->

Expand Down Expand Up @@ -451,6 +459,33 @@ impl Solution {
}
```

#### C#

```cs
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode ans = ReverseList(head.next);
head.next.next = head;
head.next = null;
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
18 changes: 9 additions & 9 deletions solution/0200-0299/0206.Reverse Linked List/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode pre = null;
for (ListNode p = head; p != null;)
{
ListNode t = p.next;
p.next = pre;
pre = p;
p = t;
ListNode dummy = new ListNode();
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = dummy.next;
dummy.next = curr;
curr = next;
}
return pre;
return dummy.next;
}
}
}
22 changes: 22 additions & 0 deletions solution/0200-0299/0206.Reverse Linked List/Solution2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode ans = ReverseList(head.next);
head.next.next = head;
head.next = null;
return ans;
}
}
2 changes: 1 addition & 1 deletion solution/0200-0299/0234.Palindrome Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ tags:

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

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

<!-- tabs:start -->

Expand Down
6 changes: 5 additions & 1 deletion solution/0200-0299/0234.Palindrome Linked List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Fast and Slow Pointers

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.

The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Loading