建立伪头指针 指向原头指针
以防止原头指针指向的节点为待删除节点
其余easy
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
faker = ListNode(head.val)
faker.next = head
if head is None:
return None#head.next is None:
cur = faker
fast = faker
for i in range(n):
fast = fast.next
if fast is None:
return None
while fast.next is not None:
fast = fast.next
cur = cur.next
todelete = cur.next
cur.next = todelete.next
return faker.next