head = [1,2,3,4,5], n = 2[1,2,3,5]Remove the node at position n when counting from the tail, starting at 1. Return the head of the remaining chain. Keep the surviving nodes in their original order; the result may be empty.
head = [1,2,3,4,5], n = 2[1,2,3,5]Array notation represents node values. The editor supplies ListNode with val and next. The inputs are a nonempty head node and a valid n, not an array passed to your function.
LeetCode 19head = [1,2,3,4,5], n = 2 result = [1,2,3,5]
Counting takes L steps and the second walk takes L - n steps. At most 2L steps means O(L) time, not O(L²).
One dummy node, a counter, and a fixed number of references use O(1) extra space.
head = [1,2,3,4,5], n = 2 result = [1,2,3,5]
Both approaches use O(L) time and O(1) extra space. The fixed gap finds the predecessor without counting the list and starting again.
Fast advances L links in total; slow advances L - n links. Each move does constant work, so time complexity is O(L).
Only two moving references and one dummy node are needed: O(1) extra space.