Question: http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/
Question Name: Remove Nth Node From End of List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # 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): former = head latter = head # Skip n-1 nodes with latter for _ in xrange(n-1): latter = latter.next if latter.next == None: # There are totally n nodes. And we are # removing the head node. head = head.next else: # There are more than n nodes. latter = latter.next # Totally skip n nodes # Find the previous node to the nth node from end while latter.next != None: former = former.next latter = latter.next # Remove the nth node from end former.next = former.next.next return head |