29. Remove every kth node
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
Traverse the linked list while keeping track of the count of nodes visited (a) and a counter b initialized to 1.
At each step, check if b * K equals a. If it does, skip the current node and increment b.
Otherwise, add the data of the current node to a vector.
After traversing the entire linked list, if the vector is empty, return nullptr since all nodes need to be deleted.
Otherwise, traverse the linked list again and replace the data of each node with the corresponding data from the vector until the end is reached.
Time Complexity : O(N)
Auxiliary Space Complexity : O(N)
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.