Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions C#/remove-nth-node-from-end-of-list.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* 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
{
// Returns a ListNode
public ListNode RemoveNthFromEnd(ListNode head, int n)
{
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode slow = dummy, fast = dummy;

for (int i = 0; i < n; i++)
{
fast = fast.next;
}

while (fast.next != null)
{
slow = slow.next;
fast = fast.next;
}

slow.next = slow.next.next;

return dummy.next;
}
}

/** SOLUTION TWO **/

public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode current = head;
Dictionary<int, ListNode> nodePositions = new();

int i = 0;
while (current != null) {
nodePositions[i] = current;
current = current.next;
i++;
}

if (i - n == 0) {
return head.next;
}

nodePositions[i - n - 1].next = nodePositions[i - n].next;

return head;
}
}