【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)
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个节点)的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 + ...
- 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 ...
- 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, ...
- 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 ...
随机推荐
- 关于启动VS2012时,提示Web4.0尚未在网络服务器上注册的解决办法!
VS2012在没有Web服务器上注册的问题,网上有很多博客. 开始我就是遇到这个问题,在网上试了几个办法,确实都还可以.但是相比之下有的需要设置DOS,进行安装.exe. 这种方法其实相比直接安装补丁 ...
- 在powerDesigner中通过SQL生成pdm
在项目需求分析的阶段,通常需要画数据库表的pdm图.有时候会直接画pdm来设计表,有时候是通过其他方式,如用纸和笔去画……当数据库中的表已经建立好了,怎么把数据库中的表导成SQL形式,然后生成pdm图 ...
- TOP100summit:【分享实录-华为】微服务场景下的性能提升最佳实践
本篇文章内容来自2016年TOP100summit华为架构部资深架构师王启军的案例分享.编辑:Cynthia 王启军:华为架构部资深架构师.负责华为的云化.微服务架构推进落地,前后参与了华为手机祥云4 ...
- Global Error Handling in ASP.NET Web API 2(webapi2 中的全局异常处理)
目前,在Web API中没有简单的方法来记录或处理全局异常(webapi1中).一些未处理的异常可以通过exception filters进行处理,但是有许多情况exception filters无法 ...
- hive优化之参数调优
1.hive参数优化之默认启用本地模式 启动hive本地模式参数,一般建议将其设置为true,即时刻启用: hive (chavin)> set hive.exec.mode.local.aut ...
- RabbitMQ 分布式设置和高可用性讨论
abbitMQ的集群主要有配置方式,分别是:本地局域网Cluster,federation,shovel. RabbitMQ Cluster主要是用于同一个网段内的局域网. federation和sh ...
- 口语详解|为什么“how to say”是错的?
你有没有说过一些印象深刻的中式英语呢?为什么有的英语会被称之为中式英语想必你大概知道,但是如何把中式英语使用正确你知道吗?今天,跟着小编来看看吧.By the way,今天的主角是"how ...
- Linux Semaphore
目录 主要用到的几个函数 0, ftok 1, semget 2, semctl 3, semop 程序semp1.c 程序semp2.c 主要用到的几个函数 0, ftok ftok - conve ...
- Web开发——JavaScript基础
参考学习: MDN JavaScript:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript ECMAScript 6入门(阮一峰):htt ...
- CALayer的子类之CAShapeLayer
一,CAShapeLayer介绍 * CAShapeLayer继承自CALayer,属于QuartzCore框架,可使用CALayer的所有属性. CAShapeLayer是在坐标系内绘制贝塞尔曲 ...