#-*- coding: UTF-8 -*-
#双指针思想,两个指针相隔n-1,每次两个指针向后一步,当后面一个指针没有后继了,前面一个指针的后继就是要删除的节点
# 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(0)
        dummy.next=head
        p=dummy
        q=dummy
        for i in range(n):
            q=q.next
        while q.next:
            p=p.next
            q=q.next
        
        rec=p.next
        p.next=rec.next
        del rec
        return dummy.next

【leetcode❤python】 19. Remove Nth Node From End of List的更多相关文章

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

  2. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

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

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

  5. 【LeetCode】19. Remove Nth Node From End of List

    题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...

  6. 【leetcode❤python】 203. Remove Linked List Elements

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  7. 【leetcode❤python】83. Remove Duplicates from Sorted List

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  8. 【leetcode❤python】27. Remove Element

    #-*- coding: UTF-8 -*- class Solution(object):    def removeElement(self, nums, val):        "& ...

  9. 【leetcode❤python】26. Remove Duplicates from Sorted Array

    #-*- coding: UTF-8 -*-class Solution(object):    def removeDuplicates(self, nums):        "&quo ...

随机推荐

  1. R12 供应商SQL

    -- 供应商主表数据: SELECT ass.vendor_id vendor_id, ass.party_id party_id, ass.segment1 vendor_code, ass.ven ...

  2. zw版【转发·台湾nvp系列Delphi例程】HALCON CropPart

    zw版[转发·台湾nvp系列Delphi例程]HALCON CropPart procedure TForm1.Button1Click(Sender: TObject);var ho_Egypt1 ...

  3. Ubentu搭建nfs服务器

    搭建nfs服务    功能:完成在线调试程序,远程挂载,在线调试    NFS(Network FileSystem,网络文件系统)是由SUN公司发展,并于1984年推出的技术,用于不同机器,不同操作 ...

  4. 华为项目管理10大模板Excel版(可直接套用_非常实用)

    项目管理是管理学的一个分支学科 ,对项目管理的定义是:指在项目活动中运用专门的知识.技能.工具和方法,使项目能够在有限资源限定条件下,实现或超过设定的需求和期望的过程.项目管理是对一些与成功地达成一系 ...

  5. combotree

    1,直接获取:     单选:$("#id").combotree("getValue")     多选:$("#id").combotre ...

  6. PHP memcached 扩展的安装

    PHP memcached 扩展的安装 1.下载相关的源码包(软件版本自己选择) memcached-1.4.15 , http://memcached.org/ //Memcached服务端. me ...

  7. 为什么Redis内存不宜过大

    redis这个内存数据库,它的高性能.稳定性都是不用怀疑的,但我们塞进redis的数据过多,内存过大,那如果出问题,那它可能会带给我们的就是灾难性. 作者:程超来源:网络|2016-05-23 09: ...

  8. JavaEE基础(二十七)/反射、JDK新特性

    1.反射(类的加载概述和加载时机) A:类的加载概述 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载  就是指将class文件读入 ...

  9. mysql 关联条件与查询(过滤)条件

    mysql用outer join时 on 后边只是关联条件,有时可能会查出无用的记录, 需用where查询条件过滤 五欧诺个的数据. 记录一下

  10. gets()和getchar()还有getch()的区别

    getch()和getchar()区别:1.getch(): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上例如: char ch;或int ch: getch();或c ...