码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-04-06 15:14:22      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

链表操作,只能遍历一遍然后。

用双指针

# 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

  

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

标签:

原文地址:http://www.cnblogs.com/colorss/p/5359131.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!