From 3965f2590ce244b3e3eb922b922e74e58cf6941d Mon Sep 17 00:00:00 2001 From: eeee0717 <70054568+eeee0717@users.noreply.github.com> Date: Thu, 25 May 2023 15:51:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0C#=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0019.删除链表的倒数第N个节点.md | 22 ++++++++++++ problems/0142.环形链表II.md | 28 +++++++++++++++ problems/0203.移除链表元素.md | 35 +++++++++++++++++++ problems/0977.有序数组的平方.md | 8 +++++ 4 files changed, 93 insertions(+) diff --git a/problems/0019.删除链表的倒数第N个节点.md b/problems/0019.删除链表的倒数第N个节点.md index c6f5bfc7..84eac96b 100644 --- a/problems/0019.删除链表的倒数第N个节点.md +++ b/problems/0019.删除链表的倒数第N个节点.md @@ -412,6 +412,28 @@ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { ``` +C#: +```csharp +public class Solution { + public ListNode RemoveNthFromEnd(ListNode head, int n) { + ListNode dummpHead = new ListNode(0); + dummpHead.next = head; + var fastNode = dummpHead; + var slowNode = dummpHead; + while(n-- != 0 && fastNode != null) + { + fastNode = fastNode.next; + } + while(fastNode.next != null) + { + fastNode = fastNode.next; + slowNode = slowNode.next; + } + slowNode.next = slowNode.next.next; + return dummpHead.next; + } +} +```
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md
index e80a715a..f87d2cd9 100644
--- a/problems/0142.环形链表II.md
+++ b/problems/0142.环形链表II.md
@@ -437,6 +437,34 @@ object Solution {
}
```
+C#:
+```CSharp
+public class Solution
+{
+ public ListNode DetectCycle(ListNode head)
+ {
+ ListNode fast = head;
+ ListNode slow = head;
+ while (fast != null && fast.next != null)
+ {
+ slow = slow.next;
+ fast = fast.next.next;
+ if (fast == slow)
+ {
+ fast = head;
+ while (fast != slow)
+ {
+ fast = fast.next;
+ slow = slow.next;
+ }
+ return fast;
+ }
+ }
+ return null;
+ }
+}
+```
+
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md
index 6a0de282..f8751658 100644
--- a/problems/0203.移除链表元素.md
+++ b/problems/0203.移除链表元素.md
@@ -596,6 +596,41 @@ class Solution {
}
```
+C#
+```CSharp
+/**
+ * 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 RemoveElements(ListNode head, int val)
+ {
+ ListNode dummyHead = new ListNode(0,head);
+ ListNode temp = dummyHead;
+ while(temp.next != null)
+ {
+ if(temp.next.val == val)
+ {
+ temp.next = temp.next.next;
+ }
+ else
+ {
+ temp = temp.next;
+ }
+ }
+ return dummyHead.next;
+ }
+}
+```
+