29. Remove every kth node
My Approach
Time and Auxiliary Space Complexity
Code (C++)
class Solution {
public:
Node* deleteK(Node *head,int K)
{
vector<int>vec;
Node* temp=head;
int a=0, b=1;
while (temp)
{
++a;
if (b*K==a)
{
b++;
temp=temp->next;
continue;
}
vec.push_back(temp->data);
temp=temp->next;
}
if (vec.size()==0)
return nullptr;
temp=head;
for (int i=0;i<vec.size();i++)
{
temp->data=vec[i];
if (i==vec.size()-1)
temp->next=nullptr;
else temp=temp->next;
}
return head;
}
};Contribution and Support
Last updated