In the Leetcode Remove Nth Node From End of List problem solution in C# programming Given the head of a linked list, remove the nth node from the end of the list and return its head.
Leetcode Remove Nth Node From End of List problem solution in C# programming
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n) {
var right = head;
for(int i = 0; i< n; i ++)
{
right = right.next;
//Reached the end
if(right == null)
{
//If there's only one node
if(head.next == null)
return null;
else
return head.next;
}
}
var left = head;
while(right.next!=null)
{
left = left.next;
right = right.next;
}
left.next = left.next.next;
return head;
}
}
Also read,
- Leetcode Remove Nth Node From End of List problem solution in C++
- Leetcode Remove Nth Node From End of List problem solution in Java
- Leetcode Remove Nth Node From End of List problem solution in Python
- Leetcode Remove Nth Node From End of List problem solution in C