您的位置:首页 > Web前端 > Node.js

Leetcode 19. Remove Nth Node From End of List(python)

2016-04-06 14:08 519 查看
链表操作,只能遍历一遍然后。

用双指针

# 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
"""
left=right=head
for i in range(n):
right=right.next
if right is None:   return head.next    #special
while right.next is not None:
right=right.next
left=left.next

left.next=left.next.next
return head


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: