Description #
Difficulty: 中等
Related Topics: Linked List, Two Pointers
Given the head
of a linked list, remove the nth node from the end of the list and return its head.
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:
- The number of nodes in the list is
sz
. 1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
Follow up: Could you do this in one pass?
Solution #
Language: Go
空间替换时间:使用 map 记录“链”。key 为链表的序号,value 记录 next。删除倒数 n 个数字时,只需要将 n-1 的 value 指向 n+1 即可。
IOI