19.删除链表的倒数第N个结点
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
root=ListNode(0) #考虑m=n=1的情况,设置哑变量
root.next=head
a=head.next
m=1
while a:
a=a.next
m+=1
a=root
i=0
while i<=m-n-1:
a=a.next
i+=1
a.next=a.next.next
return root.next
题目可以划分为2步:(1)数链表 (2)更改链表(3)考虑特殊情况