Tuesday, September 9, 2014

Leetcode: Remove Nth Node From End of List @Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @return a ListNode
    def removeNthFromEnd(self, head, n):
        fast=slow=head
        for i in range(n+1):
            if not fast:
                return head.next
            fast=fast.next
        while fast:
            fast=fast.next
            slow=slow.next
        slow.next=slow.next.next
        return head

No comments :

Post a Comment