"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = next"""classSolution:""" @param head: The first node of linked list. @param n: An integer. @return: The head of linked list. """defremoveNthFromEnd(self,head,n):# write your code hereifhead==None:returnNonelength=1tmp=headwhiletmp.next!=None:length+=1tmp=tmp.next# corner case: n == lengthifn==length:returnhead.next# remove nth lementtmp=headnum=length-n-1whilenum>0:num-=1tmp=tmp.nextiftmp.next!=None:tmp.next=(tmp.next).nextelse:tmp.next=Nonereturnhead