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