题目

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

代码:oj在线测试通过 Runtime: 188 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @return a ListNode
def removeNthFromEnd(self, head, n):
if head is None:
return head dummyhead = ListNode(0)
dummyhead.next = head
p1 = dummyhead
p2 = dummyhead for i in range(0,n):
p1 = p1.next while p1.next is not None:
p1 = p1.next
p2 = p2.next
if p1.next is None:
break p2.next = p2.next.next return dummyhead.next

思路

Linked List基本都需要一个虚表头,这道题主要思路是双指针

让第一个指针p1先走n步,然后再让p1和p2一起走;当p1走到链表最后一个元素的时候,p2就走到了倒数n+1个元素的位置;这时p2.next向表尾方向跳一个。

注意下判断条件是p1.next is not None,因此在while循环中添加判断p1.next是否为None的保护判断。

再有就是注意一下special case的情况,小白我的习惯是在最开始就把这种case都判断出来;可能牺牲了代码的简洁性,有大神路过也请拍砖指点。

leetcode 【 Remove Nth Node From End of List 】 python 实现的更多相关文章

  1. [leetcode]Remove Nth Node From End of List @ Python

    原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...

  2. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  3. [LeetCode] 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 ...

  4. LeetCode——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, Give ...

  5. Leetcode 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, Give ...

  6. [LeetCode] 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, Give ...

  7. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  8. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

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

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

随机推荐

  1. golang实现文件上传权限验证(超简单)

    Go语言创建web server非常简单,部署也很容易,不像IIS.Apache等那么重量级,需要各种依赖.配置.一些功能单一的web 服务,用Go语言开发特别适合.http文件上传下载服务,在很多地 ...

  2. prepareStatament和Statement和callableStatement的区别

    关系与区别 Statement.PreparedStatement和CallableStatement都是接口(interface) Statement 1.Statement接口提供了执行语句和获取 ...

  3. cudaMemcpy2D介绍

    cudaMemcpy2D( d_A, // 目的指针 d_pitch, // 目的pitch bmp1, // 源指针 sizeof(int)*2, // 源数据pitch sizeof(int)*2 ...

  4. 公众帐号如何向用户发送emoji表情(php版,附emoji编码表)

    //字节转Emoji表情 function bytes_to_emoji($cp) { if ($cp > 0x10000){ # 4 bytes return chr(0xF0 | (($cp ...

  5. Spring详解篇之 AOP面向切面编程

    一.概述 Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征.AOP包括切面.连接点.通知(advice).切入点(pointCut) . 1 ...

  6. AMD、CMD和CommonJS规范(转)

    CommonJS规范  CommonJS是在浏览器环境之外构建JavaScript生态系统为目标产生的项目,比如服务器和桌面环境中.CommonJS规范是为了解决JavaScript的作用域问题而定义 ...

  7. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

  8. CI的子目录控制器问题

    不管是根目录还是子目录里面的文件名必须是首字母大写,否则会报404

  9. spring-传统AOP

    Spring传统AOP AOP的增强类型 AOP联盟定义了Advice(org.aopalliance.aop.Interface.Advice) 五类(目标类方法的连接点): 1.  前置通知(or ...

  10. VS2013使用自带的数据库 Microsoft SQL Server 2012 Express LocalDB

    注:DeptLocalDB:自己取的数据库实例名称 DeptSharedLocalDB:自己取的实例共享名称np:\\.\pipe\LOCALDB#SH7C6ED5\tsql\query:命名管道名称 ...