Remove every k-th node of the linked list
Given a singly linked list, the task is to remove every kth node of the linked list. Assume that k is always less than or equal to the length of the Linked List.
Examples :
Input: LinkedList: 1 -> 2 -> 3 -> 4 -> 5 -> 6, k = 2
Output: 1 -> 3 -> 5
Explanation: After removing every 2nd node of the linked list, the resultant linked list will be: 1 -> 3 -> 5 .Input: LinkedList: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10, k = 3
Output: 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10
Explanation: After removing every 3rd node of the linked list, the resultant linked list will be: 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10.
[Expected Approach – 1] Iterative Approach – O(n) Time and O(1) Space
The idea is to traverse the linked list while maintaining a counter to track node positions. Every time the counter reaches k, update the next pointer of the previous node to skip the current kth node, effectively removing it from the list. Continue this process until reaching the end of the list. This method ensures that the kth nodes are removed as required while preserving the rest of the list structure.
Below is the implementation of the above approach:
// C++ program to delete every k-th Node of
// a singly linked list.
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to remove every kth node in the linked list
Node* deleteK(Node* head, int k) {
// If list is empty or k is 0, return the head
if (head == nullptr || k <= 0)
return head;
Node* curr = head;
Node* prev = nullptr;
// Initialize counter to track node positions
int count = 0;
// Traverse the linked list
while (curr != nullptr) {
count++;
// If count is a multiple of k, remove current node
if (count % k == 0) {
// skip the current node
if (prev != nullptr) {
prev->next = curr->next;
}
else {
head = curr->next;
}
}
else {
// Update previous node pointer only if
// we do not remove the node
prev = curr;
}
curr = curr->next;
}
return head;
}
void printList(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
}
int main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(6);
int k = 2;
head = deleteK(head, k);
printList(head);
return 0;
}
// C program to delete every k-th Node of
// a singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to remove every kth node in the linked list
struct Node* deleteK(struct Node* head, int k) {
// If list is empty or k is 0, return the head
if (head == NULL || k <= 0)
return head;
struct Node* curr = head;
struct Node* prev = NULL;
int count = 0;
while (curr != NULL) {
count++;
// If count is a multiple of k, remove
// current node
if (count % k == 0) {
// skip the current node
if (prev != NULL) {
prev->next = curr->next;
}
else {
head = curr->next;
}
free(curr);
curr = prev != NULL ? prev->next : head;
}
else {
// Update previous node pointer only if
// we do not remove the node
prev = curr;
curr = curr->next;
}
}
return head;
}
void printList(struct Node* node) {
struct Node* curr = node;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int new_data) {
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
head->next->next->next->next->next = createNode(6);
int k = 2;
head = deleteK(head, k);
printList(head);
return 0;
}
// Java program to delete every k-th Node of
// a singly linked list.
class Node {
int data;
Node next;
Node(int newData) {
data = newData;
next = null;
}
}
public class GfG {
// Function to remove every kth node in the
// linked list
static Node deleteK(Node head, int k) {
// If list is empty or k is 0, return the head
if (head == null || k <= 0)
return head;
Node curr = head;
Node prev = null;
int count = 0;
// Traverse the linked list
while (curr != null) {
// Increment the counter for each node
count++;
// If count is a multiple of k, remove
// current node
if (count % k == 0) {
// skip the current node
if (prev != null) {
prev.next = curr.next;
}
else {
// If removing the head node
head = curr.next;
}
}
else {
// Update previous node pointer only if
// we do not remove the node
prev = curr;
}
curr = curr.next;
}
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
int k = 2;
head = deleteK(head, k);
printList(head);
}
}
# Python program to delete every k-th Node of
# a singly linked list.
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Function to remove every kth node in the linked list
def delete_k(head, k):
# If list is empty or k is 0, return the head
if head is None or k <= 0:
return head
curr = head
prev = None
count = 0
while curr is not None:
count += 1
# If count is a multiple of k, remove current node
if count % k == 0:
# Bypass the current node
if prev is not None:
prev.next = curr.next
else:
# If removing the head node
head = curr.next
else:
# Update previous node pointer only if
# we do not remove the node
prev = curr
curr = curr.next
return head
def print_list(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hard-coded linked list:
# 1 -> 2 -> 3 -> 4 -> 5 -> 6
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
head.next.next.next.next.next = Node(6)
k = 2
head = delete_k(head, k)
print_list(head)
// C# program to delete every k-th Node of
// a singly linked list.
using System;
class Node {
public int Data;
public Node next;
public Node(int newData) {
Data = newData;
next = null;
}
}
class GfG {
// Function to remove every kth node in the linked list
static Node DeleteK(Node head, int k) {
// If list is empty or k is 0, return the head
if (head == null || k <= 0)
return head;
Node curr = head;
Node prev = null;
int count = 0;
while (curr != null) {
count++;
// If count is a multiple of k, remove
// current node
if (count % k == 0) {
// Bypass the current node
if (prev != null) {
prev.next = curr.next;
}
else {
// If removing the head node
head = curr.next;
}
}
else {
// Update previous node pointer only if
// we do not remove the node
prev = curr;
}
curr = curr.next;
}
return head;
}
static void PrintList(Node curr) {
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
int k = 2;
head = DeleteK(head, k);
PrintList(head);
}
}
// Javascript program to delete every k-th Node of
// a singly linked list.
class Node {
constructor(newData) {
this.data = newData;
this.next = null;
}
}
// Function to remove every kth node in the linked list
function deleteK(head, k) {
// If list is empty or k is 0, return the head
if (head === null || k <= 0) {
return head;
}
let curr = head;
let prev = null;
let count = 0;
// Traverse the linked list
while (curr !== null) {
count++;
// If count is a multiple of k, remove
// current node
if (count % k === 0) {
// skip the current node
if (prev !== null) {
prev.next = curr.next;
}
else {
// If removing the head node
head = curr.next;
}
}
else {
// Update previous node pointer only if
// we do not remove the node
prev = curr;
}
curr = curr.next;
}
return head;
}
function printList(curr) {
let output = "";
while (curr !== null) {
output += curr.data + " ";
curr = curr.next;
}
console.log(output.trim());
}
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5 -> 6
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
let k = 2;
head = deleteK(head, k);
printList(head);
Output
1 3 5
Time Complexity : O(n), where n is the number of nodes.
Auxiliary Space : O(1)