From aff7b753d2fb55d7709aae59947f0c45246e4c92 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 19 Mar 2025 18:48:59 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0206 --- .../0206.Reverse Linked List/README.md | 47 ++++++++++++---- .../0206.Reverse Linked List/README_EN.md | 55 +++++++++++++++---- .../0206.Reverse Linked List/Solution.cs | 18 +++--- .../0206.Reverse Linked List/Solution2.cs | 22 ++++++++ .../0234.Palindrome Linked List/README.md | 2 +- .../0234.Palindrome Linked List/README_EN.md | 6 +- 6 files changed, 119 insertions(+), 31 deletions(-) create mode 100644 solution/0200-0299/0206.Reverse Linked List/Solution2.cs diff --git a/solution/0200-0299/0206.Reverse Linked List/README.md b/solution/0200-0299/0206.Reverse Linked List/README.md index 13c9e67956f8a..3a47400b7a3c0 100644 --- a/solution/0200-0299/0206.Reverse Linked List/README.md +++ b/solution/0200-0299/0206.Reverse Linked List/README.md @@ -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)$。 @@ -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; } } ``` @@ -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; + } +} +``` + diff --git a/solution/0200-0299/0206.Reverse Linked List/README_EN.md b/solution/0200-0299/0206.Reverse Linked List/README_EN.md index 6ef93b442d5a7..298960c2eaa3b 100644 --- a/solution/0200-0299/0206.Reverse Linked List/README_EN.md +++ b/solution/0200-0299/0206.Reverse Linked List/README_EN.md @@ -58,7 +58,11 @@ tags: -### 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)$. @@ -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; } } ``` @@ -287,7 +291,11 @@ public class Solution { -### 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. @@ -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; + } +} +``` + diff --git a/solution/0200-0299/0206.Reverse Linked List/Solution.cs b/solution/0200-0299/0206.Reverse Linked List/Solution.cs index fd70b47359d14..41cc12eb05aee 100644 --- a/solution/0200-0299/0206.Reverse Linked List/Solution.cs +++ b/solution/0200-0299/0206.Reverse Linked List/Solution.cs @@ -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; } -} +} \ No newline at end of file diff --git a/solution/0200-0299/0206.Reverse Linked List/Solution2.cs b/solution/0200-0299/0206.Reverse Linked List/Solution2.cs new file mode 100644 index 0000000000000..c3b57890bfd3f --- /dev/null +++ b/solution/0200-0299/0206.Reverse Linked List/Solution2.cs @@ -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; + } +} \ No newline at end of file diff --git a/solution/0200-0299/0234.Palindrome Linked List/README.md b/solution/0200-0299/0234.Palindrome Linked List/README.md index c6ae9c84a2c3b..3cc316a66d7d1 100644 --- a/solution/0200-0299/0234.Palindrome Linked List/README.md +++ b/solution/0200-0299/0234.Palindrome Linked List/README.md @@ -60,7 +60,7 @@ tags: 我们可以先用快慢指针找到链表的中点,接着反转右半部分的链表。然后同时遍历前后两段链表,若前后两段链表节点对应的值不等,说明不是回文链表,否则说明是回文链表。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。 +时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。 diff --git a/solution/0200-0299/0234.Palindrome Linked List/README_EN.md b/solution/0200-0299/0234.Palindrome Linked List/README_EN.md index 579cc97d75c93..4eccdad94b92a 100644 --- a/solution/0200-0299/0234.Palindrome Linked List/README_EN.md +++ b/solution/0200-0299/0234.Palindrome Linked List/README_EN.md @@ -53,7 +53,11 @@ tags: -### 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)$.