problem
just like the title.
algorithm
inverse the link first, and then inverse it once more and ignore the Nth node.
code
# 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:
invHead = None
while head:
p = ListNode(head.val)
p.next = invHead
invHead = p
head = head.next
count = 0
ans = None
while invHead:
count += 1
if count != n:
p = ListNode(invHead.val)
p.next = ans
ans = p
invHead = invHead.next
return ans