classListNode(object):def__init__(self,x):self.val=xself.next=Noneres=ListNode(0)res.next=headtmp=res# head 1-5, tmp0-5foriinrange(0,n):# double pointer, a.head from 1 to None is 5 steps, forward 2. This means the 1st pointer firstly complete n steps. Then together with 2nd pointer, complete (total -n) steps will come to the node which will be removed.head=head.nextwhilehead!=None:# b.tmp from 0 to 5 is also 5 steps, 5-2=3(how many steps left for head), point tmp to the nth nodehead=head.nexttmp=tmp.nexttmp.next=tmp.next.nextreturnres.next
近期评论