Leetcode 19. Remove Nth Node From End of List
来源:互联网 发布:监控客户端软件 编辑:程序博客网 时间:2024/06/05 18:04
题目:
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个offset后从当前位置遍历到链表尾巴,删掉待删结点。返回第一个元素。
# 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
"""
preHead = ListNode(-1);preHead.next=head
l = preHead
for _ in xrange(n):head = head.next
while head!=None:
l=l.next;head=head.next
tmp = l.next
l.next=l.next.next
tmp = None
return preHead.next
- 19. Remove Nth Node From End of List leetcode list
- [LeetCode]19.Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List Leetcode Python
- LeetCode --- 19. Remove Nth Node From End of List
- LeetCode 19.Remove Nth Node From End of List
- [Leetcode] 19. Remove Nth Node From End of List
- [leetcode] 19.Remove Nth Node From End of List
- <LeetCode OJ> 19. Remove Nth Node From End of List
- 19. Remove Nth Node From End of List LeetCode
- leetcode 19. Remove Nth Node From End of List
- [LeetCode OJ] 19. Remove Nth Node From End of List
- LeetCode 19. Remove Nth Node From End of List
- Leetcode:19. Remove Nth Node From End of List(JAVA)
- leetcode 19. Remove Nth Node From End of List
- Leetcode 19. Remove Nth Node From End of List
- 【LeetCode】19. Remove Nth Node From End of List
- leetcode 19.Remove Nth Node From End of List
- Elasticsearch学习方法以及复杂数据类型的映射
- 一些好玩的前段动画插件
- C#【数据转换】十进制\byte[]相互转换
- 理解Python的With语句
- 剑指offer 扑克牌的顺子
- Leetcode 19. Remove Nth Node From End of List
- JAVA-9-正则表达式
- python数据挖掘实践第一章 KNN算法,以及算法的实现
- sublime插件安装配置
- 制作动态的表格js
- STL细节之Tag Dispatching
- 【深度学习】RNN中梯度消失的解决方案(LSTM)
- Java主线程等待所有子线程执行完毕再执行解决办法
- @ font-face介绍