LeetCode #19: Remove Nth Node From End of List
β’ 84 words β’ 1 min β’ updated
LeetCode #19: Remove Nth Node From End of List:
python
# 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: Optional[ListNode], n: int) -> Optional[ListNode]:
if not head:
return head
pivot = head
back = head
for _ in range(n):
if pivot:
pivot = pivot.next
if not pivot:
head = head.next
return head
while pivot.next:
pivot = pivot.next
back = back.next
if back.next:
back.next = back.next.next
return head