29. Remove every kth node
The problem can be found at the following link: Question Link
My Approach
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 and Auxiliary Space Complexity
Time Complexity :
O(N)
Auxiliary Space Complexity :
O(N)
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated