Open In App

Remove every k-th node of the linked list

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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++
// 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 Java Python C# JavaScript

Output
1 3 5 

Time Complexity : O(n), where n is the number of nodes.
Auxiliary Space : O(1)



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg