From a260aa299799cd17bd75087a63d9d16b8c730859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Velimir=20=C4=90urkovi=C4=87?= <velimir.djurkovic@gmail.com> Date: Thu, 6 Feb 2025 16:16:23 +0100 Subject: [PATCH] feat: add csharp solution to lc problem: No.0019 --- .../README.md | 34 +++++++++++++++++-- .../README_EN.md | 34 +++++++++++++++++-- .../Solution.cs | 26 ++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cs diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/README.md b/solution/0000-0099/0019.Remove Nth Node From End of List/README.md index e9931cdde2d49..151c80eb0c329 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/README.md +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/README.md @@ -280,7 +280,7 @@ var removeNthFromEnd = function (head, n) { #### Swift -````swift +```swift /** * Definition for singly-linked list. * public class ListNode { @@ -340,6 +340,37 @@ def remove_nth_from_end(head, n) end ``` +#### 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 RemoveNthFromEnd(ListNode head, int n) { + ListNode dummy = new ListNode(0, head); + ListNode fast = dummy, slow = dummy; + while (n-- > 0) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + slow.next = slow.next.next; + return dummy.next; + } +} +``` + #### PHP ```php @@ -381,4 +412,3 @@ class Solution { <!-- solution:end --> <!-- problem:end --> -```` diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md b/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md index 6c85e356c3109..3905ab6e2d5de 100644 --- a/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/README_EN.md @@ -277,7 +277,7 @@ var removeNthFromEnd = function (head, n) { #### Swift -````swift +```swift /** * Definition for singly-linked list. * public class ListNode { @@ -337,6 +337,37 @@ def remove_nth_from_end(head, n) end ``` +#### 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 RemoveNthFromEnd(ListNode head, int n) { + ListNode dummy = new ListNode(0, head); + ListNode fast = dummy, slow = dummy; + while (n-- > 0) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + slow.next = slow.next.next; + return dummy.next; + } +} +``` + #### PHP ```php @@ -378,4 +409,3 @@ class Solution { <!-- solution:end --> <!-- problem:end --> -```` diff --git a/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cs b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cs new file mode 100644 index 0000000000000..fe8e8f8c798e3 --- /dev/null +++ b/solution/0000-0099/0019.Remove Nth Node From End of List/Solution.cs @@ -0,0 +1,26 @@ +/** + * 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 RemoveNthFromEnd(ListNode head, int n) { + ListNode dummy = new ListNode(0, head); + ListNode fast = dummy, slow = dummy; + while (n-- > 0) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + slow.next = slow.next.next; + return dummy.next; + } +} \ No newline at end of file