题目简述:

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. Servlet使用注解标注监听器(Listener)

    Servlet3.0提供@WebListener注解将一个实现了特定监听器接口的类定义为监听器,这样我们在web应用中使用监听器时,也不再需要在web.xml文件中配置监听器的相关描述信息了. 下面我 ...

  2. ORA-01438: 值大于为此列指定的允许精度

    Number的数据声明如下:表示        作用        说明Number(p, s)        声明一个定点数        p(precision)为精度,s(scale)表示小数点 ...

  3. python 生成器等语法

    生成器 调用生成器函数,不会执行生成器函数中的代码,而是返回一个对象,  这个对象是生成器(可用type()函数判断这个对象类型),  如果要运行生成器函数中的代码, 需要调用 next()方法,   ...

  4. emulator control无法使用问题

    请使用Google 自带的控制器:

  5. js设置鼠标悬停改变背景色

    看了网上那么多的js鼠标悬停时事件,大多数的,说了那么多话,也没解决什么问题,现在直接贴上代码,以供参考 html: <div id="sign">this is te ...

  6. 记录分享公司Spring data相关配置

    起因 公司Spring Data相关的配置还是有点意思的,不过蛮复杂的...XML很多...我不太喜欢强行记住....所以我打算记录一下配置的大致思路和结构,以后可能会有用.... 总体结构 总体结构 ...

  7. [asp.net core]定义Tag Helpers

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/authoring Getting started wi ...

  8. C++ 操作XML文件 使用MSXML.DLL

    使用MSXML.DLL读写XML; 文件顶部加入 #import "msxml3.dll"; using namespace MSXML2; //这两句作用是,在程序的文件夹下生成 ...

  9. MySQL索引类型总结和使用技巧以及注意事项

    索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常见的MySQL索引类型 在数据库表中,对字段建立索引可以大大提高查询速度.假如我们创建了一个 mytable ...

  10. Android Notification通知详解

    根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...