referthisfirst.com Interview Question
Site ManagersTeam: Front End
Country: India
Interview Type: In-Person
C++ version with bit modification
node* modify_LL(node* head, int k)
{
node* prev = NULL;
node* current = head;
int count = 1;
while(current != NULL)
{
count += 1;
prev = current;
current = current->next;
if(count%k == 0)
{
node* temp = current;
prev->next = current->next;
free(temp);
}
}
return head;
}
Maintain two pointers previous and next and adjust those pointers accordingly depending on
whether the count % k == 0.
- prudent_programmer March 01, 2018