Given a linked list, remove the n-th node from the end of list and return its head.

Example:                     Given linked list: 1->2->3->4->5, and n = 2.                     After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:                          Given n will always be valid.

Follow up:                  Could you do this in one pass?

思路


  我们采用两个指针和哨兵模式来解决这个问题。 一开始先将快指针向前移动N位,然后和慢指针一起移动,直到指针指向最后一位结束。然后将漫指针的next指针重新赋值,就可以将对应节点删除。

  时间复杂度为O(n), 空间复杂度为O(1)。

图示步骤


解决代码


 # Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
if not head or n == 0:
return head
res = second = first = ListNode(0) # 构建哨兵节点
first.next = head
while n > 0: # 移动快指针
first = first.next
n -= 1
while first.next: # 移动快慢指针
second = second.next
first = first.next
second.next = second.next.next # 删除指定节点
return res.next

【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)的更多相关文章

  1. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  4. leetcode第19题--Remove Nth Node From End of List

    Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...

  5. LeetCode 笔记系列四 Remove Nth Node From End of List

    题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Gi ...

  6. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

  7. LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)

    题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...

  8. LeetCode题解:(19) Remove Nth Node From End of List

    题目说明 Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  9. LeetCode(19) Remove Nth Node From End of List

    题目 Given a linked list, remove the nth node from the end of list and return its head. For example, G ...

随机推荐

  1. Git版本控制工具的简单使用

    1.下载gitd客户端,注册github账号. 2.本地生成公钥和私钥,并将公钥粘贴到github上,测试连接. 3.先pull,从远程服务器中下载项目文件,然后再pushi,提交至服务器. 4. g ...

  2. Django详解之models操作

    D jango 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 sqlite3, MySQL, PostgreSQL等数据库,只需要在settings.p ...

  3. 有重复行,查询时只保留最新一行的sql

    一.表结构如下:表名test 二.sql select temp.* from (select test.*, row_number() over(partition by obd_code orde ...

  4. 【react】---context的基本使用新版---【巷子】

    一.全局定义context对象 globalContext.js import React from "react"; const GlobalContext = React.cr ...

  5. 设计模式学习--Builder

    What Builder:将一个复杂的对象的构建和表示分离,使得同样的构建过程可以创建不同的表示. Why Builder也是创建型模式的一种,它是一步一步的向导式的创建一个复杂的对象,Builder ...

  6. 02Modify.ashx(修改班级信息)

    02Modify.html 修改 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> & ...

  7. 我所知道的几种display:table-cell的应用

    .outer span {  display: table-cell; } 一.display:table-cell属性简述 display:table-cell属性指让标签元素以表格单元格的形式呈现 ...

  8. 浏览器下载Excel,直接打开显示乱码...

    情景: 浏览器中点击下载文件有两个选项:[打开][下载] [打开]之后,提示["文件.xlsx"的文件格式和扩展名不匹配.文件可能已损坏或不安全.除非您信任其来源,否则请勿打开.是 ...

  9. [No0000143]Win10“卓越性能模式”

    从不久之前Win10更新1803以来,微软不仅带来了一些新功能(和BUG),还悄悄地加入了一个“卓越性能模式”,老张想了想,不对呀,以前就有了一个“高性能模式”,这怎么就还多出个新的性能模式来,难道会 ...

  10. “对外部(局部)变量的访问”是C语言函数指针的最大弱点

    1.“对外部(局部)变量的访问”是C语言函数指针的最大弱点 . #include <stdio.h> #include <stdlib.h> /* 结构体定义 */ struc ...