i am traversing the list two time and converting it in string and comparing them and it is taking O(n) time is there any better approah
What approach other than this can be used?
@Deepanshu_garg You can split the linked list about its mid-point into two parts. Reverse one of the Linked list and then compare it with the other linked list. This way you can check if the linked list is a palindrome.
1->2->3->2->1 will split to 1->2 and 2->1, then compare 1->2 with reverse of 2->1. (Case for odd length LL)
1->2->3->3->2->1 will split to 1->2->3 and 3->2->1 and then compare it similarly. (Case for even length LL)
Hope this helps.
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.