【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,
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的更多相关文章
- 【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 ...
- 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
- 【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 ...
- 【Leetcode-easy】Remove Nth Node From End of List
思路1:设置两个指针p1,p2指向表头,p1先走n步.再两个指针同时走.当p1指针指到链表尾部时,P2指针已经在需要删除节点的前一位.一定要注意一些细节. class ListNode { int v ...
- 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 ...
- 【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 ...
- 【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: ...
- 【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 ...
- 【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 ...
随机推荐
- web api7
- 高性能异步图片加载器的JS库:lazysizes
<script src="lazysizes.min.js" async=""></script> 使用示例 <!-- non-r ...
- SQL语句判断数据库、表、字段是否存在
from master..sysdatabases where name='TestDB') print 'TestDB存在'else print 'TestDB不存在' --判断表[Te ...
- 第五章 --- 关于Javascript 设计模式 之 发布-订阅模式
先来个最简单的 发布订阅模式 document.body.addEventListener('click',function(){ alert(123); }); document.body.clic ...
- Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...
- 20155217-杨笛-c与java
命是弱者的借口,运是强者的谦词 你有什么技能比大多数人(超过90%以上)更好? 针对这个技能的获取你有什么成功的经验? 与老师博客中的学习经验有什么共同之处? 其实我经常会去想自己有什么拿得出手的优点 ...
- TCP学习之二:客户端与服务端的连接
主要参考张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html TcpClient是对Socket的封装 一个TcpClient ...
- SQL Server数据库定时自动备份
SQL Server 数据库定时自动备份[转] 在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求 ...
- Autoit3 正则表达式 匹配汉字
关于Autoit3正则匹配汉字,在网上搜来搜去都是雷同的内容,[\u4e00-\u9fa5] 然而,Invalid all the time 直到认真钻研Help File,最终又看到了这个 http ...
- Linux学习之四--Nginx
关于Nginx的 nginx的是一个高性能的Web服务器的软件.它比Apache HTTP服务器更加灵活,重量轻的程序. 本教程将教你如何安装和你的CentOS 7服务器上启动Nginx的. 先决 ...