Remove the nth node from the end
Mediumlinked listtwo pointers
Remove the nth node from the end of the list (n = 1 is the last node) and return the head. Try doing it in one pass.
Example 1
Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Example 2
Input: head = [1], n = 1 Output: []
Example 3
Input: head = [1,2], n = 1 Output: [1]
Constraints
- 1 ≤ number of nodes ≤ 30
- 0 ≤ Node.val ≤ 100
- 1 ≤ n ≤ number of nodes
Submit checks your solution against 10 tests, including hidden ones.
// Provided by the judge: keep these definitions
class ListNode {
constructor(val = 0, next = null) {
this.val = val
this.next = next
}
}
/**
* @param {ListNode|null} head
* @param {int} n
* @return {ListNode|null}
*/
function removeNthFromEnd(head, n) {
}