Alomerry Wu @ alomerry.com

19. 删除链表的倒数第 N 个结点

Sep 21, 2022 · 1min · 166 ·

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

 
 comment..
你认为这篇文章怎么样?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.0.1
Theme by antfu
2018 - Present © Alomerry Wu