Leetcode 019 Remove Nth Node From End of List

Given the head of a linked list, remove the nth node from the end of the list and return its head.


image
Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5]
Input: head = [1], n = 1
Output: []

Input: head = [1,2], n = 1
Output: [1]

  • Kod aslında açıklayıcı ve basit.
  • Bir dummy list kullanırız.
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        dummy = ListNode(0,head)
        left = dummy
        right = head
        
        while n > 0 and right:
            right = right.next
            n-= 1
        
        while right:
            left = left.next
            right = right.next
        
        left.next = left.next.next
        return dummy.next