题目简述:

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.

解题思路:

标准的双指针,第一个指针先走n步,注意删除时候的处理

# 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):
posa = ListNode(0)
posa = head
posb = ListNode(0)
posb = head
pre = ListNode(0)
pre.next = head
while n > 0:
posa = posa.next
n -= 1
while posa != None:
#print 'a',posa.val
#print 'b',posb.val
posa = posa.next
posb = posb.next
pre = pre.next
#print posb.val
if pre.next == head:
head = head.next
else:
pre.next = pre.next.next return head

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

  1. 【leetcode】Remove Nth Node From End of List(easy)

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

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

    这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...

  3. 【Leetcode】【Easy】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 ...

  4. 【Leetcode-easy】Remove Nth Node From End of List

    思路1:设置两个指针p1,p2指向表头,p1先走n步.再两个指针同时走.当p1指针指到链表尾部时,P2指针已经在需要删除节点的前一位.一定要注意一些细节. class ListNode { int v ...

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

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

  7. 【LeetCode每天一题】Remove Nth Node From End of List(移除链表倒数第N个节点)

    Given a linked list, remove the n-th node from the end of list and return its head. Example:        ...

  8. 【LeetCode OJ】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 ...

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

随机推荐

  1. Android Textview实现文字颜色渐变效果

    最近做应用的时候遇到一个需求,一行文字的颜色需要一个渐变效果 如上所有 从左到有逐渐变化,自己写了一个demo实现上述效果 package com.huwei.example.test; import ...

  2. 轻松搞定laravel的curd操作搞定简易留言版(四)

    一:目的开发laravel简易留言板 二:路由操作routes.php <?php //GET /msg/index 展示留言列表 //GET /msg/add 展示表单 //POST /msg ...

  3. TweenMax学习整理--特有属性

    TweenMax学习整理--特有属性   构造函数:TweenMax(target:Object, duration:Number, vars:Object) target:Object -- 需要缓 ...

  4. C# 构建动态树

    public class Tree { public Guid Id { get; set; } public string Url { get; set; } public Guid? Parent ...

  5. git用法之[回滚代码]

    我们在写代码的任何过程中,都有可能出错,任何过程都有可能要!回!滚!代!码!事关重大!一定要详细讲讲. 一.关于 工作区.暂存区.本地分支: 工作区:即自己当前分支所修改的代码,git add xx ...

  6. Gcc的Makefile简单使用

    Gcc的Makefile简单使用http://blog.chinaunix.net/uid-9330295-id-2425867.html

  7. python3 黑板客爬虫闯关游戏(三)

    第三关,先登录,再猜密码,这关难度较第二关大幅增加,要先去注册一个登录账号,然后打开F12,多登录几次,观察headers数据的变化 给出代码,里面注释很详细 import urllib.reques ...

  8. powershell使用

    主要语法点: -match -notmatch -replace -join -split -and -or -xor -not ! +.-.*./.% =.+=.-=.*=./=.%= -eq.-n ...

  9. Android 获得AndroidManifest文件里自定义的meta标签内容

    try { ApplicationInfo appInfo= this.getPackageManager().getApplicationInfo(getPackageName(),PackageM ...

  10. 前端模拟 图片上传---->>通过选取的图片获取其路径<<------

    <head> <meta charset="UTF-8"> <title>Title</title> <style> d ...