1474. Delete N Nodes After M Nodes of a Linked List

https://leetcode.com/problems/delete-n-nodes-after-m-nodes-of-a-linked-list/

class Solution:
    def deleteNodes(self, head: ListNode, m: int, n: int) -> ListNode:
        dummy = ListNode(-1)
        dummy.next = head
        curr = dummy
        while curr:
            mCount = m
            nCount = n
            while curr and mCount:
                curr = curr.next
                mCount-=1
            
            while curr and curr.next and nCount:
                curr.next = curr.next.next
                nCount-=1
        return dummy.next
        

Last updated

Was this helpful?