## Introduction

This blog will discuss the various approaches to solve the Remove every Kth node of the linked list problem. Before jumping into the problem to remove every Kth node of the linked list, letâ€™s first understand a Linked list.

A Linked list is a kind of data structure in which the elements are connected using pointers, and each node has the address of the pointer of its next node.

You can refer to this link for more information on the linked list.

In this Remove every Kth node of the linked list problem, we need to return the head of the modified linked list after removing all the nodes whose position is the multiple of â€˜Kâ€™ from the linked list.

__For Example:__

List :- 10 -> 15 -> 10 -> 5 -> 145 -> 15 -> 5 -> 11 -> 47 -> 10 -> 15 -> 75, K = 3

Output :- 10 -> 15 -> 5 -> 145 -> 5 -> 11 -> 10 -> 15

## Approach

The Solution considers checking the complete linked list by traversing the whole linked list and deleting every Kth node of the linked list.

**Note:- **K is always less than or equal to the length of the Linked list.

### Algorithm

**Step 1**. Create a function â€˜getResult()â€™ that will accept two parameters, i.e., one â€˜headâ€™ pointer of the linked list, and the value of â€˜Kâ€™.

**Step 2. **Check for the base case with the help of an â€˜Ifâ€™ condition if the value of â€˜headâ€™ is â€˜nullâ€™, then we need to return â€˜nullâ€™.

**Step 3. **Check with the help of an â€˜Ifâ€™ condition if the value of â€˜Kâ€™ is equal to 1, then we need to delete the â€˜headâ€™ of the Linked list and return the â€˜nullâ€™ value.

**Step 4**. Initialize three variables: â€˜tempâ€™ will keep track of the elements of the linked list, â€˜previousâ€™ will keep track of the previous node of the â€˜tempâ€™, and one variable â€˜countâ€™ will keep track of the count of the â€˜tempâ€™.

**Step 5**. Assign the value of â€˜headâ€™ to â€˜tempâ€™ and assign the null value to â€˜previousâ€™, and â€˜countâ€™ with zero.

**Step 6**. Make an iteration using the â€˜whileâ€™ loop, which will terminate if the value of â€˜temp1â€™ is null.

**Step 7.** Increment the â€˜countâ€™.

**Step 8. **Check if the value of â€˜countâ€™ is equal to â€˜Kâ€™, which means it is the â€˜Kthâ€™ node that must be deleted.

- If it is the â€˜Kthâ€™ node, then assign the value of the â€˜nextâ€™ pointer of the â€˜tempâ€™ node to the â€˜nextâ€™ pointer of the â€˜previousâ€™ node and make the value of â€˜countâ€™ equal to zero.

**Step 9. **If â€˜countâ€™ is not equal to zero, then assign the value of â€˜tempâ€™ to the â€˜previousâ€™ variable and assign the value of the â€˜nextâ€™ pointer of â€˜previousâ€™ to â€˜tempâ€™.

**Step 10. **Return the value of the head.

### Implementation in C++

```
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
Node *takeinput()
{
int data;
cin >> data;
Node *head = NULL, *tail = NULL;
while (data != -1)
{
Node *newNode = new Node(data);
if (head == NULL)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
cin >> data;
}
return head;
}
// Function used to delete the node
void freeList(Node *node)
{
while (node != NULL)
{
Node *next = node -> next;
delete(node);
node = next;
}
}
Node *getResult(Node *head, int k)
{
// If linked list has no node
if (head == NULL)
{
return NULL;
}
if (k == 1)
{
freeList(head);
return NULL;
}
Node *temp = head, *previous = NULL;
// Traverse list
int count = 0;
while(temp != NULL)
{
count++;
// If that particular 'temp' node is required Kth node
if (k == count)
{
Node *store = temp -> next;
delete(previous -> next);
previous -> next = store;
count = 0;
}
// update previous if count is not 0
if (count != 0)
{
previous = temp;
}
temp = previous -> next;
}
return head;
}
/* Function to print the linked list */
void print(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp -> data << " ";
temp = temp -> next;
}
return;
}
int main()
{
Node *head = takeinput();
print(head);
cout << endl;
int k = 4;
head = getResult(head, k);
print(head);
}
```

```
Output :
Given Linked List:- 10 -> 15 -> 10 -> 5 -> 145 -> 15 -> 5 -> 11 -> 47 -> 10 -> 15 -> 75
Modified Linked List:- 10 -> 15 -> 10 -> 145 -> 15 -> 5 -> 47 -> 10 -> 15
```

### Complexity Analysis

**Time Complexity: **O(N)

In call to â€˜getResult()â€™, As we are traversing the whole linked list which of the length â€˜Nâ€™ only once, therefore, the overall time complexity is O(N).

**Space Complexity: **O(1)

As we are using constant space, therefore, the overall space complexity will be O(1).