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的更多相关文章

  1. 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, ...

  2. 【leetcode❤python】 19. Remove Nth Node From End of List

    #-*- coding: UTF-8 -*-#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点# Definition for sin ...

  3. leetcode个人题解——#19 Remove Nth Node From End of List

    思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...

  4. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

  5. 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. ...

  6. 刷题19. Remove Nth Node From End of List

    一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...

  7. 【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 ...

  8. 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 + ...

  9. [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 ...

  10. 【一天一道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 ...

随机推荐

  1. 工作中apache 403的一个小问题

    最近在虚拟机上安装hadoop, 需要设备本地的网络源,所以启用了apache. 由于需要,首先修改了家目录的位置 指向/opt/www   然后修改家目录的配置文件 修改完成之后重启服务,访问目录 ...

  2. mongo启动报错问题处理

    关键错误信息child process failed, exited with error number 100 这是服务器断电导致数据库意外关闭导致的问题,处理方法也比较简单 rm -rf /var ...

  3. c++ 数组 结构体

    接下来的一点时间我将会记录下我看的c++的一些心得体会,人贵在坚持,希望我可以一直坚持下去!!Go Fighting!   一.c++复合数据类型: 数组类型的一些注意事项: sizeof的用法: 当 ...

  4. PHP强制修改返回的状态码

    在最后的程序执行完毕之前,加入下列语句,即可实现所有的返回码都为200即使在服务器内部发生错误,会报500情况下只要加上register_shutdown_function函数的处理同样可以实现返回2 ...

  5. 简单xml示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...

  6. 14Filter&Listener

    1.Filter:过滤器 1.1 概念 生活中的过滤器:净水器,空气净化器,土匪. web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能. 过滤器的作用: 一般用于完成 ...

  7. Java语言基础(6)

    1 while循环 案例:Demo1 1+2+3+4+5+...+100 = ? 首先定义一个变量sum,用来保存累加之和,int sum=0 第1次:sum = sum + 1 第2次: sum = ...

  8. 基于Twemproxy的Redis集群方案(转载)

    原文地址:基于Twemproxy的Redis集群方案 概述 由于单台redis服务器的内存管理能力有限,使用过大内存redis服务器的性能急剧下降,且服务器发生故障将直接影响大面积业务.为了获取更好的 ...

  9. UVALive - 5695 The Last Puzzle (思维+区间dp)

    题目链接 题目大意:有n个按钮排成一条直线,你的任务是通过左右移动按下所有按钮,按钮如果一段时间没有被按下就会被弹开. 以下是我的推论(不一定正确): 直观地看的话,如果选择的是最优路径,那么路径的形 ...

  10. [洛谷P4602] CTSC2018 混合果汁

    问题描述 小 R 热衷于做黑暗料理,尤其是混合果汁. 商店里有 n 种果汁,编号为 0, 1, 2, . . . , n − 1.i 号果汁的美味度是 di,每升价格为 pi.小 R 在制作混合果汁时 ...