Linked List January 28 ,2026

Remove Every K-th Node in a Linked List

Problem Statement

You are given a singly linked list and an integer K. Your task is to remove every K-th node from the linked list.

Key Rule

  • Counting starts from the first node
  • Every K-th node must be deleted
  • The remaining nodes should preserve their original order

Understanding the Problem Clearly

Let us understand the problem using an example.

Example 1

Input Linked List:

1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL

K = 3

Deletion Process

  • Remove 3rd node → 3
  • Remove 6th node → 6
  • Remove 9th node → does not exist

Output Linked List

1 → 2 → 4 → 5 → 7 → 8 → NULL

Important Edge Cases

Before jumping into solutions, we must handle edge cases:

  1. K = 1
    • Every node must be deleted
    • Output will be an empty list
  2. K > Length of Linked List
    • No node is deleted
    • List remains unchanged
  3. Empty Linked List
    • Nothing to delete
  4. Linked List with Single Node
    • Deleted only if K = 1

Approach 1: Iterative Counting Approach (Most Common)

Idea

We traverse the linked list while maintaining:

  • A counter to track node positions
  • A previous pointer to reconnect nodes after deletion

Whenever the counter becomes a multiple of K, the current node is removed.

Algorithm

  1. If head is NULL, return head.
  2. If K == 1, delete the entire list.
  3. Initialize:
    • current = head
    • previous = NULL
    • count = 1
  4. Traverse the list:
    • If count % K == 0:
      • Delete current
      • Update previous.next
    • Else:
      • Move previous forward
    • Increment count
  5. Return updated head.

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C Implementation

// while running your code please replace " of "stdio.h" with < and > respectively
#include "stdio.h"
#include "stdlib.h"


struct Node {
    int data;
    struct Node* next;
};

// Create new node
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Insert at end
struct Node* insertEnd(struct Node* head, int data) {
    struct Node* newNode = createNode(data);

    if (head == NULL)
        return newNode;

    struct Node* temp = head;
    while (temp->next != NULL)
        temp = temp->next;

    temp->next = newNode;
    return head;
}

// Display list
void display(struct Node* head) {
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}

// Remove every K-th node
struct Node* removeKthNode(struct Node* head, int k) {
    if (head == NULL)
        return NULL;

    if (k == 1)
        return NULL;

    struct Node* current = head;
    struct Node* previous = NULL;
    int count = 1;

    while (current != NULL) {
        if (count % k == 0) {
            struct Node* temp = current;
            previous->next = current->next;
            current = current->next;
            free(temp);
        } else {
            previous = current;
            current = current->next;
        }
        count++;
    }

    return head;
}

int main() {
    struct Node* head = NULL;

    // Input: 10 → 20 → 30 → 40 → 50
    int arr[] = {10, 20, 30, 40, 50};
    for (int i = 0; i < 5; i++) {
        head = insertEnd(head, arr[i]);
    }

    printf("Original List:\n");
    display(head);

    int k = 2;
    head = removeKthNode(head, k);

    printf("\nAfter removing every %d-th node:\n", k);
    display(head);

    return 0;
}

Java Implementation

class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class RemoveKthNode {

    static Node removeKthNode(Node head, int k) {
        if (head == null)
            return null;

        if (k == 1)
            return null;

        Node current = head;
        Node previous = null;
        int count = 1;

        while (current != null) {
            if (count % k == 0) {
                previous.next = current.next;
            } else {
                previous = current;
            }
            current = current.next;
            count++;
        }
        return head;
    }
}

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def remove_kth_node(head, k):
    if head is None:
        return None

    if k == 1:
        return None

    current = head
    previous = None
    count = 1

    while current:
        if count % k == 0:
            previous.next = current.next
        else:
            previous = current
        current = current.next
        count += 1

    return head

Dry Run Example

Input

List: 10 → 20 → 30 → 40 → 50 → NULL
K = 2

Step-by-Step

NodeCountAction
101Keep
202Delete
303Keep
404Delete
505Keep

Output

10 → 30 → 50 → NULL

Common Mistakes Students Make

  1. Forgetting to handle K = 1
  2. Losing reference to head
  3. Incorrect pointer updates
  4. Not freeing memory (C/C++)
  5. Infinite loop due to incorrect traversal

Alternative Approach (Using Length)

Idea

  • Calculate the length first
  • Delete nodes at positions K, 2K, 3K...

 Less efficient due to two traversals
Used mainly for conceptual understanding

Interview Tips

  • Interviewers expect O(n) time and O(1) space
  • Always mention edge cases
  • Explain pointer movement clearly
  • Dry run is more important than code

10. Summary

  • Removing every K-th node requires careful pointer manipulation
  • Iterative approach is optimal
  • Handling K = 1 is critical
  • This problem strengthens traversal and deletion concepts

Practice Variations

Try solving:

  • Remove every alternate node
  • Remove nodes at prime positions
  • Remove nodes with even values
  • Remove nodes greater than X


Next Problem in the Series

 Middle of a Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com