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 ...
随机推荐
- TVM:一个端到端的用于开发深度学习负载以适应多种硬件平台的IR栈
TVM:一个端到端的用于开发深度学习负载以适应多种硬件平台的IR栈 本文对TVM的论文进行了翻译整理 深度学习如今无处不在且必不可少.这次创新部分得益于可扩展的深度学习系统,比如 TensorFlo ...
- jquery ready load
jq 加载三种写法 $(document).ready(function() { // ...代码... }) //document ready 简写 $(function() { // ...代码. ...
- 【版本控制工具】 Git基础
一.Git简介 Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.于是Git 成了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件. (Git目前使用率非常 ...
- osworkflow 入门基础
OSWorkFlow入门指南目的 这篇指导资料的目的是介绍OSWorkflow的所有概念,指导你如何使用它,并且保证你逐步理解OSWorkflow的关键内容. 本指导资料假定你已经部署OSWorkfl ...
- hive创建分区表
#创建分区表CREATE TABLE if not exists data_center.test_partition (id int,name string,age int)PARTITIONED ...
- html背景图不随滚轮滚动,而且按住Ctrl并滚动滚轮时,图片不会变大缩小,就像百度的首页一样
之前在百度知道我提问过这一个问题,后来解决了.不过好多人来问我时怎么解决的,源码.其实很简单.这里我贴一下代码.有需要的小伙伴不用再加我qq了,直接来这里取吧. 里面的图片是我随便找的. <!D ...
- 通过实现接口runnable实现多线程
实现Runnable接口实现多线程的步骤(1)编写类实现Runnable接口(2)实现run(方法(3)通过Thread类的start(方法启动线程 静态代理模式Thread >代理 角色MyR ...
- dirname命令和basename命令
dirname返回文件所在目录路径,而basename则相反,去掉路径返回最后的文件名. direname: 用途 从给定的包含绝对路径的文件名中去除文件名(非目录的部分),然后返回剩下的路径(目录的 ...
- Struts2-Action接受参数方式、method属性使用及通配符的配置
一.Action接受参数的方式 1.属性方式接收 首先编写一个用于上传参数的页面 <%@ page contentType="text/html;charset=UTF-8" ...
- border-box与content-box的区别
㈠box-sizing 属性 ⑴box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. ⑵语法:box-sizing: content-box|border-box|inherit; ...