# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defremoveNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: res = ListNode(-1) dummy = res h_val = [] while head: h_val.append(head.val) head = head.next for i inrange(len(h_val)):
if i == len(h_val) - n: continue res.next = ListNode(h_val[i]) res = res.next