GoCalf Calf 的自留地 · 昙花一现

19. Remove Nth Node From End of List

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:

case1
case1

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

python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
solution_test.py下载完整源码
py
import pytest

from node import ListNode
from solution import Solution


@pytest.mark.parametrize('values, n, expected', [
    ([1,2,3,4,5], 2, [1,2,3,5]),
    ([1], 1, []),
    ([1,2], 1, [1]),

    ([1,2,3,4,5], 1, [1,2,3,4]),
    ([1,2,3,4,5], 5, [2,3,4,5]),
])
class Test:
    def test_solution(self, values, n, expected):
        sol = Solution()
        head = self._build_list(values)
        res_head = sol.removeNthFromEnd(head, n)
        result = self._format(res_head)
        assert result == expected

    def _build_list(self, values: list[int]) -> ListNode | None:
        root = ListNode()
        node = root
        for v in values:
            node.next = node = ListNode(v)

        return root.next

    def _format(self, head: ListNode | None) -> list[int]:
        values = []
        while head is not None:
            values.append(head.val)
            head = head.next

        return values

Thoughts

两个指针 p1、p2 都指向链表头,让 p2 先走 n 步,然后与 p1 一起往前走。每次让 p2 先走,没到的话再让 p1 走,这样当 p2 走到头的时候,p1 指向倒数第 n 个节点的前一个。

给链表前边加一个虚拟节点,指向 head,在处理边界的时候会方便很多。

Code

py
from typing import Optional

from node import ListNode

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next


class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        root = ListNode(None, head)
        p1 = p2 = root
        for _ in range(n):
            p2 = p2.next

        while True:
            p2 = p2.next
            if p2 is None:
                break
            p1 = p1.next

        p1.next = p1.next.next

        return root.next

评论需要 JavaScript。