Problem
Given the head
of a linked list, remove the nᵗʰ
node from the end of the list and return its head.
https://leetcode.com/problems/remove-nth-node-from-end-of-list/
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
Test Cases
1 | # Definition for singly-linked list. |
1 | import pytest |
Thoughts
两个指针 p1、p2 都指向链表头,让 p2 先走 n 步,然后与 p1 一起往前走。每次让 p2 先走,没到的话再让 p1 走,这样当 p2 走到头的时候,p1 指向倒数第 n 个节点的前一个。
给链表前边加一个虚拟节点,指向 head,在处理边界的时候会方便很多。
Code
1 | from typing import Optional |