Remove Every K-th Node Of The Linked List


In this article, we will explain the way to remove every k-th node of the linked list. We must delete every node that sits on the multiple of k, i.e., we have to delete the node on positions k, 2*k, 3*k, etc.

Input : 112->231->31->41->54->63->71->85
   k = 3
Output : 112->231->41->54->71->85
Explanation: As 3 is the k-th node after its deletion list would be :
First iteration :112->231->41->54->63->71->85
Now we count from 41 the next kth node is 63
After the second iteration our list will become : 112->231->41->54->71->85
And our iteration continues like this.

Input: 14->21->23->54->56->61
   k = 1
Output: Empty list
Explanation: All nodes need to be deleted

In this problem, we will apply a normal approach that is efficient enough so that we don’t require to optimize it.

Approach to find The Solution

In this problem, we are going to traverse the linked list with a counter. If the counter hits k, we delete that node and refresh the counter to find the next element in the kth position from the current node.

Example

#include<bits/stdc++.h>
using namespace std;
/* Linked list Node */
struct Node {
   int data;
   struct Node* next;
};
void push(struct Node** ref, int new_data) { // pushing the data into the list

   struct Node* new_n = new Node;
   new_n->data = new_data;
   new_n->next = (*ref);
   (*ref) = new_n;
}
void deletek(Node* prev, Node* curr) { // delete function

   if(prev == NULL) {
      prev = curr;
      curr = curr -> next;
      free(prev);
      prev = NULL;
   } else {
      prev -> next = curr -> next;
      auto tmp = curr;
      free(tmp); // freeing the space
   }
}
/* Function to print linked list */
void displayList(struct Node *head) {
   struct Node *temp = head;
   while (temp != NULL) {
      cout<<temp->data<<" ";
      temp = temp->next;
   }
}
// Function to create a new node.
struct Node *newNode(int x) {
   Node *temp = new Node;
   temp->data = x;
   temp->next = NULL;
   return temp;
}
int main() {
   struct Node* head = NULL;
   push(&head, 80);
   push(&head, 70);
   push(&head, 60);
   push(&head, 50);
   push(&head, 40);
   push(&head, 30);
   push(&head, 20);
   int k = 3; // given k
   Node* curr = head; // current pointer
   Node* prev = NULL; // previous pointer
   int count = 1; // position counter
   if(head == NULL || k == 0) // if list is already empty or k = 0
      cout << "Invalid\n";
   else {
      while(curr) { // traversing the list
         if(count == k) {
            deletek(prev, curr);
            curr = prev -> next;
            count = 1;
         } else {
            count++;
            prev = curr;
            curr = curr -> next;
         }
      }
      displayList(head); // printing the new list
   }
   return 0;
}

Output

20 30 50 60 80

The above approach has a time complexity of O(N), where N is the size of our given linked list.

Explanation of the above code

In the above approach, we keep three things in hand first, the current pointer, second the previous pointer, and third the position counter. Now we delete some node when our position counter becomes equal to know we call the function to delete with a previous and current counter as parameters now we delete the current node and free up space now when the delete function is done now we shift current pointer to the next element and refresh our counter to 1 and loop this block till our current becomes NULL.

Conclusion

In this article, we solve a problem to Remove every k-th node of the linked list. We also learned the C++ program for this problem and the complete approach ( Normal) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.

Updated on: 26-Nov-2021

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements