removeNthFromEndRecursive<T> function
Removes the nth node from the end using recursive approach
head - The head of the linked list
n - The position from the end (1-indexed)
Returns the new head of the linked list
Time Complexity: O(n) where n is the length of the list Space Complexity: O(n) due to recursion stack
Implementation
LinkedListNode<T>? removeNthFromEndRecursive<T>(
LinkedListNode<T>? head,
int n,
) {
if (head == null || n <= 0) return head;
int count = 0;
return _removeNthFromEndHelper(head, n, count);
}