This is my code
I am getting a TLE
You are getting TLE because your linked list formation is O(n*n), it can be done in O(n) if you have access to tail pointer directly.
Gunit do it this way!, your approach to form linked list is of O(n*n) as every to find tail you have to travel across whole linked list
But in your code you haven’t inserted the node at tail (in main)
and still not getting the right answer from your code
Gunit whenever I add a new node, then I’m already at last, so I don’t need to calculate it again!! this is what you require else your complexity if N^2. Also I’ve tested the code at hackerblocks and it is running fine!
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.