[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, 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个节点。
解题思路:加一个头结点dummy,并使用双指针p1和p2。p1先向前移动n个节点,然后p1和p2同时移动,当p1.next==None时,此时p2.next指的就是需要删除的节点。
代码:
# 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):
dummy=ListNode(0); dummy.next=head
p1=p2=dummy
for i in range(n): p1=p1.next
while p1.next:
p1=p1.next; p2=p2.next
p2.next=p2.next.next
return dummy.next
[leetcode]Remove Nth Node From End of List @ Python的更多相关文章
- LeetCode: Remove Nth Node From End of List 解题报告
Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- leetcode remove Nth Node from End python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
随机推荐
- 20169211《Linux内核原理与分析》 第十周作业
一.Linux内核之进程地址空间学习总结 Linux内核除了要管理物理内存还需要管理虚拟内存.用户进程的地址空间就是虚拟内存的一部分.每个用户进程都独有一个地址空间.由于是虚拟化的内存,所以从每个进程 ...
- Spring单例 和 Scope注解
关键字 @Scope @Qualifier Singleton 单例 Spring是单例模式.结合Springboot的例子. Controller @Autowired private Tes ...
- android viewHolder static 静态
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 不是静态内部类 会 持有 外部类的 引用. 就像经常自定义的 适配器 类 作为内部类 ...
- bzoj3456 城市规划 多项式求In
\(n\)个点的无向联通图的个数 打着好累啊 一定要封装一个板子 记\(C(x)\)为无向图个数的指数型生成函数,\(C(0) = 1\) 记\(G(x)\)为无向联通图个数的指数型生成函数,\(G( ...
- Xcode更新后插件失效解决办法
defaults read /Applications/Xcode.app/Contents/Info DVTPlugInCompatibilityUUID 获取新版xcode的uuid Xcode6 ...
- 设置Nginx开机自启动
Nginx 是一个很强大的高性能Web和反向代理服务器.虽然使用命令行可以对nginx进行各种操作,比如启动等,但是还是根据不太方便.下面介绍在linux下安装后,如何设置开机自启动. 首先,在lin ...
- 读书笔记_Effective_C++_条款三十六:绝不重新定义继承而来的non-virtual函数
这个条款的内容很简单,见下面的示例: class BaseClass { public: void NonVirtualFunction() { cout << "BaseCla ...
- 普天通信JavaEE开发岗面试题
1 EJB中有几种Bean,叙述有状态Bean和无状态Bean的差别. 答:EJB中有Session Bean,Entity Bean,以及 Message Driven Bean.这两种的 Sess ...
- 22LINQ查询运算符返回IEnumerable<T>实例汇总
本篇体验LINQ的各种查询运算符. 先创建一个泛型方法,用来显示查询结果: private static void DisplayQuery<T>(IEnumerable<T&g ...
- 转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发
活动指示器(UIActivityIndicatorView)可以告知用户有一个操作正在进行中.进度指示器(UIProgressView )也具有同样功能,而且还可以告知用户离操作结束还多远. 这两个指 ...