Delete N Nodes After M Nodes of a Linked List — LeetCode 1474 Python Solution

EasyLeetCode PremiumLinked List
Problem
#1474
Reading time
4 min

The problem

You are given the head of a linked list and two integers m and n. Traverse the linked list and remove some nodes in the following way: Start with the head as the current node.

Example

Input
head = [1,2,3,4,5,6,7,8,9,10,11,12,13], m = 2, n = 3
Output
[1,2,6,7,11,12]
Explanation
Keep the first (m = 2) nodes starting from the head of the linked List (1 ->2) show in black nodes.

Python solution

Python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteNodes(self, head: ListNode, m: int, n: int) -> ListNode:
        pre = head
        while pre:
            for _ in range(m - 1):
                if pre:
                    pre = pre.next
            if pre is None:
                return head
            cur = pre
            for _ in range(n):
                if cur:
                    cur = cur.next
            pre.next = None if cur is None else cur.next
            pre = pre.next
        return head

Complexity

MeasureComplexity
TimeO(n), where n is the number of nodes in the linked list
SpaceO(1) auxiliary

Pattern: Linked List

Rewire pointers in place, with a dummy head and a saved next to keep it safe. LeetCode 1474. Delete N Nodes After M Nodes of a Linked List is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Linked List.

The linked list guide has the Python template for the pattern and the 75 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1474. Delete N Nodes After M Nodes of a Linked List?
LeetCode 1474. Delete N Nodes After M Nodes of a Linked List is rated Easy on LeetCode.
What is the time complexity of LeetCode 1474. Delete N Nodes After M Nodes of a Linked List?
The Python solution on this page runs in O(n), where n is the number of nodes in the linked list.
What is the space complexity of LeetCode 1474. Delete N Nodes After M Nodes of a Linked List?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1474. Delete N Nodes After M Nodes of a Linked List cover?
LeetCode 1474. Delete N Nodes After M Nodes of a Linked List is tagged Linked List on LeetCode.
Is LeetCode 1474. Delete N Nodes After M Nodes of a Linked List a premium problem?
Yes. LeetCode 1474. Delete N Nodes After M Nodes of a Linked List is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview