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?

 # 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
"""
dummy = ListNode(-1)
dummy.next = head
cur, slow = dummy, dummy
while n > 0:
cur = cur.next
n -= 1
while cur.next is not None:
slow = slow.next
cur = cur.next
slow.next = slow.next.next
return dummy.next

最新文章

  1. 对​O​p​e​n​C​V​直​方​图​的​数​据​结​构​C​v​H​i​s​t​o​g​r​a​m​的​理​解
  2. CGCDSSQ
  3. BC68(HD5606) 并查集+求集合元素
  4. Uploadify 3.2 not work in IE10
  5. 【BZOJ】1016: [JSOI2008]最小生成树计数 深搜+并查集
  6. CSS排版页面
  7. HDU 3835 R(N)(枚举)
  8. 自己动手搭建苹果推送Push服务器
  9. 关于generator异步编程的理解以及如何动手写一个co模块
  10. 重绘(redraw或repaint),重排(reflow)
  11. Column 'id' in where clause is ambiguous
  12. ng环境搭建步骤
  13. 爬虫_拉勾网(解析ajax)
  14. Source Code Pro 编程字体
  15. (转) Learning Deep Learning with Keras
  16. float四舍五入保留一位小数点(坑)
  17. SQL作业
  18. 20145314郑凯杰《网络对抗技术》恶意DLL注入进程(进程捆绑)的实现
  19. CentOS配置本地yum源/阿里云yum源/163yuan源,并配置yum源的优先级
  20. alloc retain release函数

热门文章

  1. 为什么声明了int型的变量并且直接初始化后,int型变量的地址一直在变化?
  2. 计算机网络(7): 传输层TCP和UDP以及TCP的工作方式
  3. RTMP、RTSP
  4. 85.常用的返回QuerySet对象的方法使用详解:defer,only
  5. Linux系统相关命令
  6. 成为优秀Angular开发者所需要学习的19件事
  7. sublime3激活方法
  8. HNOI2018/AHOI2018 游戏
  9. Java之多线程方式一(继承Thread类)
  10. oracle 学习(四)游标