Remove Every k'th Node in the Linked List

Last Updated : 13 Aug, 2026

Given a singly linked list head , The task is to remove every kth node from the linked list.

Examples: 

Input: k = 2

input

Output:

output

Explanation: After removing every 2nd node of the linked list, the resultant linked list will be: 1 -> 3 -> 5 .

Input: k = 3

input2

Output:

output_2

Explanation: After removing every 3rd node of the linked list, the resultant linked list will be: 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10.

Try It Yourself
redirect icon

[Naive Approach] Using Recursive Traversal - O(n) Time and O(n/k) 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.

Working of Approach:

  • Start from the current node and move K - 1 nodes ahead.
  • If fewer than K nodes remain, return the current list.
  • Delete the K-th node and connect the previous node to the next node.
  • Recursively process the list starting after the deleted node.
  • Return the modified linked list.
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *deleteK(Node *head, int K)
{
    // If the list is empty, return the head.
    if (head == nullptr)
        return head;

    Node *temp = head;
    Node *prev = nullptr;

    // Move to the K-th node.
    for (int i = 1; i < K && temp != nullptr; i++)
    {
        prev = temp;
        temp = temp->next;
    }

    // If fewer than K nodes remain, return the list.
    if (temp == nullptr)
        return head;

    // Delete the K-th node.
    if (prev == nullptr)
    {
        head = temp->next;
    }
    else
    {
        prev->next = temp->next;
    }

    // Store the next node before deleting.
    Node *nextNode = temp->next;

    // Free memory.
    delete temp;

    // Recursively process the remaining list.
    if (prev == nullptr)
        return deleteK(nextNode, K);

    prev->next = deleteK(nextNode, K);

    return head;
}

void printList(Node *head)
{
    // Traverse and print the linked list.
    while (head != nullptr)
    {
        cout << head->data;

        if (head->next != nullptr)
            cout << " -> ";

        head = head->next;
    }

    cout << endl;
}

int main()
{
    // Create the linked list.
    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);
    head->next->next->next->next->next->next = new Node(7);
    head->next->next->next->next->next->next->next = new Node(8);
    head->next->next->next->next->next->next->next->next = new Node(9);
    head->next->next->next->next->next->next->next->next->next = new Node(10);

    int K = 3;

    head = deleteK(head, K);

    printList(head);

    return 0;
}
Java
import java.util.*;

class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node deleteK(Node head, int K)
    {
        // If the list is empty, return the head.
        if (head == null)
            return head;

        Node temp = head;
        Node prev = null;

        // Move to the K-th node.
        for (int i = 1; i < K && temp != null; i++) {
            prev = temp;
            temp = temp.next;
        }

        // If fewer than K nodes remain, return the list.
        if (temp == null)
            return head;

        // Delete the K-th node.
        if (prev == null) {
            head = temp.next;
        }
        else {
            prev.next = temp.next;
        }

        // Store the next node before deleting.
        Node nextNode = temp.next;

        // Free memory.
        temp = null;

        // Recursively process the remaining list.
        if (prev == null)
            return deleteK(nextNode, K);

        prev.next = deleteK(nextNode, K);

        return head;
    }

    public static void printList(Node head)
    {
        // Traverse and print the linked list.
        while (head != null) {
            System.out.print(head.data);

            if (head.next != null)
                System.out.print(" -> ");

            head = head.next;
        }

        System.out.println();
    }

    public static void main(String[] args)
    {
        // Create the linked list.
        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);
        head.next.next.next.next.next.next = new Node(7);
        head.next.next.next.next.next.next.next
            = new Node(8);
        head.next.next.next.next.next.next.next.next
            = new Node(9);
        head.next.next.next.next.next.next.next.next.next
            = new Node(10);

        int K = 3;

        head = deleteK(head, K);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def deleteK(head, K):
    # If the list is empty, return the head.
    if head is None:
        return head

    temp = head
    prev = None

    # Move to the K-th node.
    for i in range(1, K):
        if temp is None:
            break
        prev = temp
        temp = temp.next

    # If fewer than K nodes remain, return the list.
    if temp is None:
        return head

    # Delete the K-th node.
    if prev is None:
        head = temp.next
    else:
        prev.next = temp.next

    # Store the next node before deleting.
    nextNode = temp.next

    # Free memory.
    temp = None

    # Recursively process the remaining list.
    if prev is None:
        return deleteK(nextNode, K)

    prev.next = deleteK(nextNode, K)

    return head


def printList(head):
    # Traverse and print the linked list.
    while head is not None:
        print(head.data, end="")

        if head.next is not None:
            print(" -> ", end="")

        head = head.next

    print()


if __name__ == "__main__":

    # Create the linked list.
    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)
    head.next.next.next.next.next.next = Node(7)
    head.next.next.next.next.next.next.next = Node(8)
    head.next.next.next.next.next.next.next.next = Node(9)
    head.next.next.next.next.next.next.next.next.next = Node(10)

    K = 3

    head = deleteK(head, K)

    printList(head)
C#
using System;

public class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node deleteK(Node head, int K)
    {
        // If the list is empty, return the head.
        if (head == null)
            return head;

        Node temp = head;
        Node prev = null;

        // Move to the K-th node.
        for (int i = 1; i < K && temp != null; i++) {
            prev = temp;
            temp = temp.next;
        }

        // If fewer than K nodes remain, return the list.
        if (temp == null)
            return head;

        // Delete the K-th node.
        if (prev == null) {
            head = temp.next;
        }
        else {
            prev.next = temp.next;
        }

        // Store the next node before deleting.
        Node nextNode = temp.next;

        // Free memory.
        temp = null;

        // Recursively process the remaining list.
        if (prev == null)
            return deleteK(nextNode, K);

        prev.next = deleteK(nextNode, K);

        return head;
    }

    public static void printList(Node head)
    {
        // Traverse and print the linked list.
        while (head != null) {
            Console.Write(head.data);

            if (head.next != null)
                Console.Write(" -> ");

            head = head.next;
        }

        Console.WriteLine();
    }

    public static void Main()
    {
        // Create the linked list.
        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);
        head.next.next.next.next.next.next = new Node(7);
        head.next.next.next.next.next.next.next
            = new Node(8);
        head.next.next.next.next.next.next.next.next
            = new Node(9);
        head.next.next.next.next.next.next.next.next.next
            = new Node(10);

        int K = 3;

        head = deleteK(head, K);

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

function deleteK(head, K)
{
    // If the list is empty, return the head.
    if (!head)
        return head;

    let temp = head;
    let prev = null;

    // Move to the K-th node.
    for (let i = 1; i < K && temp; i++) {
        prev = temp;
        temp = temp.next;
    }

    // If fewer than K nodes remain, return the list.
    if (!temp)
        return head;

    // Delete the K-th node.
    if (!prev) {
        head = temp.next;
    }
    else {
        prev.next = temp.next;
    }

    // Store the next node before deleting.
    let nextNode = temp.next;

    // Free memory.
    temp = null;

    // Recursively process the remaining list.
    if (!prev)
        return deleteK(nextNode, K);

    prev.next = deleteK(nextNode, K);

    return head;
}

function printList(head)
{
    // Traverse and print the linked list.
    let current = head;
    while (current) {
        process.stdout.write(current.data);
        if (current.next)
            process.stdout.write(" -> ");
        current = current.next;
    }
    console.log();
}

// Driver Code
// Create the linked list.
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);
head.next.next.next.next.next.next = new Node(7);
head.next.next.next.next.next.next.next = new Node(8);
head.next.next.next.next.next.next.next.next = new Node(9);
head.next.next.next.next.next.next.next.next.next
    = new Node(10);

let K = 3;

head = deleteK(head, K);

printList(head);

Output
1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10

[Expected Approach] Using Position-Based Single Traversal - O(n) Time and O(1) Space

The idea is to traverse the linked list while tracking positions. Whenever the next node is at a position divisible by K, delete it by updating the current node’s next pointer.

Working of Approach:

  • Start with temp at the head and count = 1.
  • Check whether temp->next is the K-th node.
  • If yes, remove temp->next by changing the link.
  • Otherwise, move temp to the next node.
  • Continue until the end of the linked list.

Let us understand with an example:
Input: k = 3

input2
  • Start with temp = 1 and count = 1. Since (1 + 1) % 3 != 0, move temp to 2.
  • Now count = 2. Since (2 + 1) % 3 == 0, the next node 3 is the 3rd node, so delete 3.
  • After deletion, keep temp at 2 and increment count to 3.
  • Continue traversing the original positions. The nodes at positions 6 and 9 are also deleted.
  • The final linked list is 1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10.
output_2
C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *deleteK(Node *head, int K)
{

    // Check if the list is empty or K is 1
    if (head == nullptr || K == 1)
        return nullptr;

    // Create a temporary node to traverse the list
    Node *temp = head;

    // Initialize a counter for counting nodes
    int count = 1;

    while (temp != nullptr && temp->next != nullptr)
    {

        // If next node is the K-th node
        if ((count + 1) % K == 0)
        {

            Node *nodeToDelete = temp->next;

            // Skip the K-th node
            temp->next = nodeToDelete->next;

            // Free memory
            delete nodeToDelete;
        }
        else
        {

            // Move to the next node
            temp = temp->next;
        }

        count++;
    }

    // Return the modified list
    return head;
}

void printList(Node *head)
{
    // Traverse and print the linked list.
    while (head != nullptr)
    {
        cout << head->data;
        if (head->next != nullptr)
            cout << " -> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{

    // Create the linked list.
    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);
    head->next->next->next->next->next->next = new Node(7);
    head->next->next->next->next->next->next->next = new Node(8);
    head->next->next->next->next->next->next->next->next = new Node(9);
    head->next->next->next->next->next->next->next->next->next = new Node(10);

    int K = 3;

    head = deleteK(head, K);

    printList(head);

    return 0;
}
Java
import java.util.*;

class Node {
    int data;
    Node next;

    Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    static Node deleteK(Node head, int K)
    {
        // Check if the list is empty or K is 1
        if (head == null || K == 1)
            return null;

        // Create a temporary node to traverse the list
        Node temp = head;

        // Initialize a counter for counting nodes
        int count = 1;

        while (temp != null && temp.next != null) {
            // If next node is the K-th node
            if ((count + 1) % K == 0) {
                Node nodeToDelete = temp.next;

                // Skip the K-th node
                temp.next = nodeToDelete.next;

                // Free memory
                nodeToDelete = null;
            }
            else {
                // Move to the next node
                temp = temp.next;
            }

            count++;
        }

        // Return the modified list
        return head;
    }

    static void printList(Node head)
    {
        // Traverse and print the linked list.
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" -> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        // Create the linked list.
        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);
        head.next.next.next.next.next.next = new Node(7);
        head.next.next.next.next.next.next.next
            = new Node(8);
        head.next.next.next.next.next.next.next.next
            = new Node(9);
        head.next.next.next.next.next.next.next.next.next
            = new Node(10);

        int K = 3;

        head = deleteK(head, K);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


def deleteK(head, K):
    # Check if the list is empty or K is 1
    if head is None or K == 1:
        return None

    # Create a temporary node to traverse the list
    temp = head

    # Initialize a counter for counting nodes
    count = 1

    while temp is not None and temp.next is not None:
        # If next node is the K-th node
        if (count + 1) % K == 0:
            nodeToDelete = temp.next

            # Skip the K-th node
            temp.next = nodeToDelete.next

            # Free memory
            nodeToDelete = None
        else:
            # Move to the next node
            temp = temp.next

        count += 1

    # Return the modified list
    return head


def printList(head):
    # Traverse and print the linked list.
    while head is not None:
        print(head.data, end="")
        if head.next is not None:
            print(" -> ", end="")
        head = head.next
    print()


if __name__ == "__main__":
    # Create the linked list.
    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)
    head.next.next.next.next.next.next = Node(7)
    head.next.next.next.next.next.next.next = Node(8)
    head.next.next.next.next.next.next.next.next = Node(9)
    head.next.next.next.next.next.next.next.next.next = Node(10)

    K = 3

    head = deleteK(head, K)

    printList(head)
C#
using System;

public class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node deleteK(Node head, int K)
    {
        // Check if the list is empty or K is 1
        if (head == null || K == 1)
            return null;

        // Create a temporary node to traverse the list
        Node temp = head;

        // Initialize a counter for counting nodes
        int count = 1;

        while (temp != null && temp.next != null) {
            // If next node is the K-th node
            if ((count + 1) % K == 0) {
                Node nodeToDelete = temp.next;

                // Skip the K-th node
                temp.next = nodeToDelete.next;

                // Free memory
                nodeToDelete = null;
            }
            else {
                // Move to the next node
                temp = temp.next;
            }

            count++;
        }

        // Return the modified list
        return head;
    }

    public static void printList(Node head)
    {
        // Traverse and print the linked list.
        while (head != null) {
            Console.Write(head.data);
            if (head.next != null)
                Console.Write(" -> ");
            head = head.next;
        }
        Console.WriteLine();
    }

    public static void Main()
    {
        // Create the linked list.
        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);
        head.next.next.next.next.next.next = new Node(7);
        head.next.next.next.next.next.next.next
            = new Node(8);
        head.next.next.next.next.next.next.next.next
            = new Node(9);
        head.next.next.next.next.next.next.next.next.next
            = new Node(10);

        int K = 3;

        head = deleteK(head, K);

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

function deleteK(head, K)
{
    // Check if the list is empty or K is 1
    if (head === null || K === 1)
        return null;

    // Create a temporary node to traverse the list
    let temp = head;

    // Initialize a counter for counting nodes
    let count = 1;

    while (temp !== null && temp.next !== null) {
        // If next node is the K-th node
        if ((count + 1) % K === 0) {
            let nodeToDelete = temp.next;

            // Skip the K-th node
            temp.next = nodeToDelete.next;

            // Free memory
            nodeToDelete = null;
        }
        else {
            // Move to the next node
            temp = temp.next;
        }

        count++;
    }

    // Return the modified list
    return head;
}

function printList(head)
{
    // Traverse and print the linked list.
    let current = head;
    while (current !== null) {
        process.stdout.write(current.data.toString());
        if (current.next !== null)
            process.stdout.write(" -> ");
        current = current.next;
    }
    console.log();
}

// Driver Code
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);
head.next.next.next.next.next.next = new Node(7);
head.next.next.next.next.next.next.next = new Node(8);
head.next.next.next.next.next.next.next.next = new Node(9);
head.next.next.next.next.next.next.next.next.next
    = new Node(10);

let K = 3;

head = deleteK(head, K);

printList(head);

Output
1 -> 2 -> 4 -> 5 -> 7 -> 8 -> 10
Comment