class Solution:
# @return a ListNode
def removeNthFromEnd(self, head, n):
if head:
next=head<span style="color:#ff0000;">#此节点先向后移动n</span>
pre=head<span style="color:#ff0000;">#此节点记录删除节点前一个节点</span>
now=head<span style="color:#ff0000;">#记录删除节点</span>
#pdb.set_trace()
while n>0 and next!=None:
next=next.next
n-=1
if n>0:
return None
while next!=None:
pre=now
now=now.next
next=next.next
if pre==now:<span style="color:#ff0000;">#删除节点恰好为头结点</span>
head=head.next
return head
else:
pre.next=now.next
return head
else:
return None
Python学习——leetcode(Remove Nth Node From End of List)
最新推荐文章于 2022-10-29 13:05:33 发布