The problem. Given the head of a linked list, remove the nth node from the end and return the head. n = 1 means the last node.
Input: head = [1, 2, 3, 4, 5], n = 2
Output: [1, 2, 3, 5]
Input: head = [1], n = 1
Output: []Two difficulties: a singly linked list can't count backwards from the end, and deleting a node means changing the arrow of the node before it. When the node to delete is the head, there is no node before it — a dummy node fixes that.
Free account
Sign up to read the rest of this lesson: 5 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come