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,
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个元素,况且只能遍历链表一遍。
所以,需要定义两个指针,相差是N个元素,同时向前遍历,当靠前的元素走到最后的时候,在后面的元素刚好与最后一个元素距离为N,删除即可。
于是:
#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
"""
p = head
q = head
if head.next ==None:return []
for i in range(0,n):
q = q.next
if not q : return head.next
while q.next:
p = p.next
q = q.next
temp = p.next.next
p.next = temp
return head if __name__=='__main__':
arr = [1,2]
p = ListNode(arr[0])
head = p
for i in arr[1:]:
p.next = ListNode(i)
p = p.next
s=Solution()
q=s.removeNthFromEnd(head,2)
while q:
print (q.val)
q = q.next
19. Remove Nth Node From End of List的更多相关文章
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...
- 刷题19. Remove Nth Node From End of List
一.题目说明 这个题目是19. Remove Nth Node From End of List,不言自明.删除链表倒数第n个元素.难度是Medium! 二.我的解答 链表很熟悉了,直接写代码. 性能 ...
- 【LeetCode】19. Remove Nth Node From End of List (2 solutions)
Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...
- [LeetCode] 19. 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 19] Remove Nth Node From End of List
1 题目 Given a linked list, remove the nth node from the end of list and return its head. For example, ...
- Java [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 ...
- 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, Give ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
随机推荐
- centos
CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linu ...
- python基础-RE正则表达式
re 正则表示式 正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写 ...
- 面向对象和面向过程的js版选项卡
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 微信跳转浏览器来下载不同系统的app
在微信里面,是不能通过应用宝以外的方式去直接下载app的,但是却可以通过跳转到浏览器去下载app,因此如果刚好各位公司有刚刚上线的app,来不及放到微信应用宝那里,可以试试这种办法. 实现思路: 1. ...
- Maven_profile_使用profile配置不同环境的properties(实践)
配置方法分为以下几个步骤: 1.配置profiles节点(pom.xml) 2.配置build节点(pom.xml)--如果不配置该节点则无法找到profile中的properties属性值,并且配置 ...
- 为Debian/Ubuntu的apt-get install添加自动补齐/完成功能
Debian/Ubuntu的apt-get太常用了,不过偶尔可能也会碰到不太熟悉,想不起来的包的名称,除了去debian packages去查找,另外的方法就是给Debian/Ubuntu添加自动补齐 ...
- Maven随记
如何保持依赖的多个jar保持版本一致 在引入依赖的时候常常需要依赖多个独立的模块, 譬如Spring的content, aop等等, 为了保持版本一致, 可以设置<spring.version& ...
- Fxx and game
可提交的传送门http://acm.hdu.edu.cn/showproblem.php?pid=5945 分析:这道题目可以采用动态规划来解决 设f[i]表示把i变成1的最小代价. 所以有:f[i] ...
- COGS729. [网络流24题] 圆桌聚餐
«问题描述:假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri(i=1,2,3...m), .会议餐厅共有n张餐桌,每张餐桌可容纳c i(i=1,2...n) 个代表就餐.为了 ...
- Hibernate的session缓存和对象的四种状态
一.session缓存 说session缓存就得说到JAVA对象的生命周期,当没有任何引用指向一个对象时,对象则可以被gc回收,也就是生命周期结束了 而hibernate获取一个对象后,会将对象存入s ...