Remove Nth Node From End of List
来源:互联网 发布:js解析复杂json数据 编辑:程序博客网 时间:2024/05/16 15:41
题目:
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.
代码:
此题思路简单,不再赘述。只是没有一把通过,说明思路还不够严谨,须努力。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param {ListNode} head
# @param {integer} n
# @return {ListNode}
def removeNthFromEnd(self, head, n):
length = 0
tmp = head
while tmp:
length += 1
tmp = tmp.next
sentry = ListNode(-1)
sentry.next=head
tmp = sentry
for i in range(length-n):
tmp=tmp.next
tmp.next = tmp.next.next
return sentry.next
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node from End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- Remove Nth Node From End of List
- 14.正规化
- 将博客搬至51CTO
- Android 框架炼成 教你如何写组件间通信框架EventBus
- monkeyrunner 学习笔记(按键学习)
- oracle所有的管理命令,包括对耗时的语句的查找
- Remove Nth Node From End of List
- 产生N个不重复的随机数的快速算法
- Android EventBus实战 没听过你就out了
- 20150618-Django之models个人心得
- C++语言中随机数的使用
- linux目录权限
- Asp.net Mvc4默认权限详细 【转】
- java乱码问题
- Android开发教程--对float数据取整