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;
+ }
+}
+```
+