题目来源:

https://leetcode.com/problems/remove-nth-node-from-end-of-list/


题意分析:

  这道题是给定一个链表,删除倒数第n个节点。提醒,1.输入的链表长度必然大于n,2.尽量通过访问一次就得到结果。


题目思路:

  这道题的问题在于如何找到倒数第n个节点。由于只能访问一次,所以可以建立两个链表,tmp1和tmp2。tmp1先访问第一个到n个节点。这时候,tmp2从0开始访问。等tmp1访问结束的时候,tmp2刚好访问到倒数第n个节点。


代码(python):

 # 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
"""
ans = ListNode(0);
ans.next = head
tmp1 = ans
tmp2 = ans
i = 0
while i < n:
tmp1 = tmp1.next
i += 1
while tmp1.next:
tmp1 = tmp1.next
tmp2 = tmp2.next
tmp2.next = tmp2.next.next
return ans.next

转载请注明出处:http://www.cnblogs.com/chruny/p/4844007.html

[LeetCode]题解(python):019-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 019 Remove Nth Node From End of List

    题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...

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

  4. 【LeetCode】019. 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. 【JAVA、C++】LeetCode 019 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第19题--Remove Nth Node From End of List

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

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

  8. 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, Gi ...

  9. 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, G ...

  10. [Leetcode][019] Remove Nth Node From End of List (Java)

    题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...

随机推荐

  1. rails总结

    rails总结 注意:本文档以rails3.2版本为基础,并且用RubyMine 4.0.3作为ide 一.rails的结构与重要文件 Rails 是一个MVC库.同时,Rails的特点就是:惯用名优 ...

  2. BF、KMP、BM、Sunday算法讲解

    BF.KMP.BM.Sunday算法讲解 字串的定位操作通常称作串的模式匹配,是各种串处理系统中最重要的操作之一. 事实上也就是从一个母串中查找一模板串,判定是否存在. 现给出四种匹配算法包括BF(即 ...

  3. Ubuntu嵌入式开发环境配置问题集锦(不断更新)

    本文章主要记录在建立嵌入式开发环境中遇到的各种问题,并详细写上解决方法.     我的开发环境为:win7+Vmware9.0+Ubuntu12.04     之所以选择这样的开发环境是因为:1. 有 ...

  4. IOS框架概览

    iOS是执行在iPhone.iPod Touch或iPad上的操作系统,之前叫做iPhone OS,iOS与Mac OS X有共同的基础架构和底层技术.但iOS是依据移动设备的特点而设计的,所以和Ma ...

  5. 不是技术牛人,如何拿到国内IT巨头的Offer(1)

    转自:http://developer.51cto.com/art/201404/436685.htm 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人,N ...

  6. 最近用的到的一些js的常用方法(简单的)

    由于新的项目开始了,是使用MVC 5.0 开发的,前端使用了两个主流的框架 UIKIT,Ignite UI(收费) 因为是mvc主要用json来交互,不能避免要对前端脚本进行操作,所以就将能用到的方法 ...

  7. iOS实践03

    主要目标:版本新特性界面,新浪授权界面(登录界面)的处理 任务基本完成了,基本的框架也就到这了,接下来的应该是首页获取微博了. 1.版本新特性,可以单独作为一个model,写完之加入到项目中.我们新建 ...

  8. HDU3535-AreYouBusy

    描述: As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, ...

  9. Mac OS X 下 TAR.GZ 方式安装 MySQL 5.7+

     方法: http://www.widlabs.com/article/mac-os-x-install-mysql-57-with-tar-gz  mysql下载地址:http://www.mysq ...

  10. google浏览器的安装

    很简单,命令行下安装命令:apt-get install google-chrome-stable(如安装不成功,输入apt-get -f install进行修复依赖,之后在菜单里就可以看到图标了) ...