[LC] 19. Remove Nth Node From End of List
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
[LC] 19. Remove Nth Node From End of List的更多相关文章
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- [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 ...
- 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
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- Java [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, Give ...
随机推荐
- Python时间问题
获取当前的时间,time只能精确到秒,而datetime可以精确到毫秒,所以使用格式化的时候要注意. nowTime=time.localtime((time.time())) t=time.strf ...
- re模块3
#分组 () print(re.findall("(ad)/(vv)","adddad/vvdddddddddd")) print(re.findall(&qu ...
- 落地即王道,锁死企业智变CP——云+AI
国庆前夜,"70年,我是主角"的微电影引发网络热议,这是人民日报新媒体和电影频道联手打造.京东云和京东AI提供技术支持.这是中国首部全民定制国庆献礼片,网友只要上传正脸照片,就能通 ...
- bootstrap 支持的JavaScript插件
一次性导入: Bootstrap提供了一个单一的文件,这个文件包含了Bootstrap的所有JavaScript插件,即bootstrap.js(压缩版本:bootstrap.min.js). 具体使 ...
- Linux-线程常见函数
1.线程创建与回收 (1).pthread_create 主线程用来创造子线程 (2).pthread_join 主线程用来等待(阻塞)回收子线程 (3).pthread_detach 主线程用来 ...
- OpenMP笔记(二)
原文:https://www.bearoom.xyz/2019/02/18/openmp2/ OpenMP是由三部分组成的:指令.库函数和环境变量. 一.指令 在C/C++中使用OpenMP需要用到的 ...
- go语言实现leetcode-242
package main import ( "fmt" "reflect" ) func isAnagram(s string, t string) bool ...
- 计算机网络(7): 传输层TCP和UDP以及TCP的工作方式
UDP:无连接:不保证可靠:面向报文的: TCP:面向连接:提供可靠交付:面向字节流(把应用层的数据分包,每个包装一些字节:不关心应用层给的包多大,而是根据网络状况,窗口大小决定) TCP报文: 序号 ...
- 微信公众号关联小程序AppID是什么
微信公众平台appid在哪 1.appid和appsecret是微信公众平台服务号才有的,如果自己家的公众平台不是服务号,需要升级为服务号. 2.登录服务号,登录“服务”条目,“服务中心”如图. 3. ...
- php 随机useragent
<?php /** * 获取随机useragent */ private function get_rand_useragent($param) { $arr = array( 'Mozilla ...