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∎
Connections
Shared tags