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. 02 Linux常用基本命令(二)

    1.Linux的文件系统格式 1.以 / 为根目录,成树状结构分布 2.查看根目录下有什么 ls / 3./下有超级用户root的家目录(root),还有普通用户的家目录(/home) 4.常用文件夹 ...

  2. API接口之安全篇

    APP.前后端分离项目都采用API接口形式与服务器进行数据通信,传输的数据被偷窥.被抓包.被伪造时有发生,那么如何设计一套比较安全的API接口方案呢? 一般的解决方案如下: 1.Token授权认证,防 ...

  3. jq无限极树结构

    //群组树结构$(function () { var params= { "companyId":cmpId }; var loadUrl="/apiv2/classif ...

  4. enum:python实现枚举也很优雅

    介绍 enum是一个用来枚举的模块 创建枚举类型 import enum # 创建一个类,继承自enum下的Enum class Color(enum.Enum): red = 1 green = 2 ...

  5. libusb-test

    /******************************************************************************** * * File Name : li ...

  6. 清北学堂提高组突破营游记day3

    讲课人更换成dms. 真的今天快把我们逼疯了.. 今天主攻数据结构, 基本上看完我博客能理解个大概把, 1.LCA 安利之前个人博客链接.之前自己学过QWQ. 2.st表.同上. 3.字符串哈希.同上 ...

  7. 对比MySQL,你究竟在什么时候更需要MongoDB(转载)

    你期望一个更高的写负载 万美元的交易. 不可靠环境保证高可用性 设置副本集(主-从服务器设置)不仅方便而且很快,此外,使用MongoDB还可以快速.安全及自动化的实现节点(或数据中心)故障转移. 未来 ...

  8. char()和VARCHAR()的主要区别是什么?

    1.char的长度是不可变的,而varchar的长度是可变的 字段b:类型char(10),     值为:abc,存储为:abc             (abc+7个空格) 字段d:类型varch ...

  9. thinkphp5权限仿制

    权限列表 流程 thinkphp5封装好的权限模块 RBAC还有auth and then .......管理员表,可以依据auth.php搭建所有的权限表

  10. [洛谷P2605] ZJOI2016 基站选址

    问题描述 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄不超过Si的范 ...