Link list K append

one of the tast case shows TLE , Can u plz help !!

@ashigupta.gupta570
First of all handle the special case when k is a multiple of n separately. After k = k % n , k would become 0 for such a case. In this case , do nothing and simply return the original list.
Also , another reason I could think of for your TLE is that your input function takes O(n^2) time. Everytime you have to insert a new element , you always iterate towards the end of the list all the way. This can easily be done in O(n) time. Try to optimise this.

ya but i m not able to optimize

@ashigupta.gupta570
While building the list , you traverse to the tail of the list and then for the next element , you start again by traversing all the way to the tail again. There is absolutely no need to do this. Instead write a loop and remember the tail pointer and just put the next element in front to it. No need to iterate all the way to it again everytime.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.