leetcode-easy-listnode-19 remove nth node from end of list
mycode 88.29%
关键是一定要head前新建一个节点,否则要分类讨论很多次来避免slow或者fast出现None.next的错误
# 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
slow = fast = dummy
#print(n,head.val)
while n:
fast = fast.next
n -= 1
#print(n,head.val,fast)
while fast and fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
例如,下面这种情况就要分情况讨论,但是会更快
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def removeNthFromEnd(self, head, n):
fast = slow = head
for _ in range(n):
fast = fast.next
if not fast: #例如1-》2-》3-》4-》None,n=4或者5的时候,删除的就应该是第一个节点,所以返回head.next就好
return head.next
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return head
leetcode-easy-listnode-19 remove nth node from end of list的更多相关文章
- 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❤python】 19. Remove Nth Node From End of List
#-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...
- leetcode个人题解——#19 Remove Nth Node From End of List
思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 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. ...
- 刷题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解题报告—— 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. 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
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
随机推荐
- python中同步、多线程、异步IO、多线程对IO密集型的影响
目录 1.常见并发类型 2.同步版本 3.多线程 4.异步IO 5.多进程 6.总结 1.常见并发类型 I/ O密集型: 蓝色框表示程序执行工作的时间,红色框表示等待I/O操作完成的时间.此图没有按比 ...
- php函数之substr()
问题: 希望从字符串的某个特定位置开始抽取这个字符串的一部分.例如,对于输入到一个表单的用户名,想要得到这个用户名的前8个字符. 解决: 使用substr()选择子串 $substring = sub ...
- mysql 命令行登录详解
mysql -?或者是mysql --help或者是mysql -I显示mysql命令的选项. #最全的mysql登录方式: mysql -hIP -uUSENME -pPWD -PPORT DBNA ...
- Ubuntu Linux 查看、编辑、比较二进制文件
查看二进制有以下几种方法: 方法一:hexdump apt-get install libdata-hexdumper-perl 安装好之后就可以直接hexdump your_binary_file ...
- Python 3.8测试阶段正式开始,发布Beta 1版
上周,Python背后的团队宣布发布了Python 3.8.0b1 版本,这是Python 3.8计划的四个beta发行预览版中的第一个.此版本标志着beta阶段的开始,您可以在此阶段测试新特性,并使 ...
- linux PHP空间设置GZIP压缩网页方法!
网站设置GZIP压缩,缩小网页体积,让网站访问速度更快!方法很简单,只需要在你的网站根目录建立.htaccess文件并输入以下代码: 新建-记事本-复制以下代码:(如果网站已有.htaccess文件, ...
- 使用TortoiseGit,设置ssh方式连接git仓库。
开始设置之前的准备:建立项目文件夹,初始化git仓库(右键 git init),右键打开 git bash ,git pull “仓库地址”, 把网站上的仓库代码拉取下来. TortoiseGit使 ...
- 29.Jwt集成(3):token设置过期时间、异常判断
token设置过期时间 package main import ( "fmt" "github.com/dgrijalva/jwt-go" "io/i ...
- MyEclipse使用教程:使用工作集组织工作区
[MyEclipse CI 2019.4.0安装包下载] 工作集允许您通过过滤掉不关注的项目来组织项目视图.激活工作集时,只有分配给它的项目才会显示在项目视图中. 如果您的视图中有大量项目,这将非常有 ...
- RocketMQ和Kafka的差异对比
Broker差异 主从差异: kafka的master/slave是基于partition维度的,而rocketmq是基于broker维度的:kafka的master/slave是可以切换的,而roc ...