Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to delete n nodes after m nodes from a linked list in Python
Suppose we are given a linked list that has the start node as "head", and two integer numbers m and n. We have to traverse the list and delete some nodes such that the first m nodes are kept in the list and the next n nodes after the first m nodes are deleted. We perform this until we encounter the end of the linked list. We start from the head node, and the modified linked list is to be returned.
The linked list structure is given to us as ?
Node
value : <integer>
next : <pointer to next node>
So, if the input is like elements = [1, 2, 3, 4, 5, 6, 7, 8], m = 3, n = 1, then the output will be [1, 2, 3, 5, 6, 7].
Algorithm
To solve this, we will follow these steps ?
Initialize prev and curr pointers to head
Use a counter to track nodes traversed
-
While curr is not null, do:
Count nodes until we reach m
When counter equals m, skip the next n nodes
Reset counter and continue
Return the modified head
Implementation
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def make_list(elements):
if not elements:
return None
head = ListNode(elements[0])
current = head
for element in elements[1:]:
current.next = ListNode(element)
current = current.next
return head
def print_list(head):
result = []
ptr = head
while ptr:
result.append(str(ptr.val))
ptr = ptr.next
print('[' + ', '.join(result) + ']')
def delete_n_after_m(head, m, n):
if not head or m <= 0:
return head
current = head
while current:
# Skip m nodes
for i in range(m - 1):
if current is None:
return head
current = current.next
if current is None:
return head
# Delete n nodes
for i in range(n):
if current.next is None:
break
current.next = current.next.next
# Move to next group
current = current.next
return head
# Test the implementation
elements = [1, 2, 3, 4, 5, 6, 7, 8]
head = make_list(elements)
print("Original list:")
print_list(head)
result = delete_n_after_m(head, 3, 1)
print("After deleting 1 node after every 3 nodes:")
print_list(result)
Original list: [1, 2, 3, 4, 5, 6, 7, 8] After deleting 1 node after every 3 nodes: [1, 2, 3, 5, 6, 7]
Example with Different Parameters
# Example: Delete 2 nodes after every 2 nodes
elements = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
head = make_list(elements)
print("Original list:")
print_list(head)
result = delete_n_after_m(head, 2, 2)
print("After deleting 2 nodes after every 2 nodes:")
print_list(result)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After deleting 2 nodes after every 2 nodes: [1, 2, 5, 6, 9, 10]
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time | O(L) | L is the length of the linked list |
| Space | O(1) | Only using constant extra space |
Conclusion
This algorithm efficiently deletes n nodes after every m nodes in a linked list by traversing the list once. The key insight is to keep m nodes, then skip n nodes by adjusting pointers, repeating until the end of the list is reached.
