From 7f261075d14374238956e750bef15abae021d7e3 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 29 May 2024 14:10:49 +0800 Subject: [PATCH] 20240529 Finished --- LeetCodeSharp/Problems/Solution19.cs | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 LeetCodeSharp/Problems/Solution19.cs diff --git a/LeetCodeSharp/Problems/Solution19.cs b/LeetCodeSharp/Problems/Solution19.cs new file mode 100644 index 0000000..438903c --- /dev/null +++ b/LeetCodeSharp/Problems/Solution19.cs @@ -0,0 +1,54 @@ +// [19] Remove Nth Node From End of List + +using LeetCodeSharp.Utils; + +namespace LeetCodeSharp.Problems19 +{ + // Submission codes start here + + /** + * 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) + { + var dummyNode = new ListNode(-1) + { + next = head + }; + + var precursor = dummyNode; + var now = head; + var count = 0; + + while (now.next != null) + { + if (count < n - 1) + { + count += 1; + } + else + { + precursor = precursor.next; + } + + now = now.next; + } + + precursor.next = precursor.next.next; + + return dummyNode.next; + } + } + + // Submission codes end here +} \ No newline at end of file