


The lists (1 -> 2 -> 1), (3 -> 4 -> 4-> 3), and (1) are palindromes, while the lists (1 -> 2 -> 3) and (3 -> 4) are not.
The first and only line contains the linked list of elements separated by a single space and terminated by -1.
Hence -1 would never be a list element.
Print “True” if the given linked list is a palindrome. Else, print “False”.
You do not need to print anything.
It has already been taken care of. Just implement the given function.
The idea is to store the list values in a stack and then compare the values in the list with the values in the stack.
The idea is to reach the end of the list and then compare if the last node has the same value as the first node, the second last node has the same value as the second node and so on..
For comparing node values we will use a the recursion stack.
For example, if the given list is (1 -> 2 -> 3 -> 2 -> 1).
First we will traverse the list upto last node. Now, when we are coming out of the Nth recursive state, list (1 -> 2 -> 3 -> 2 -> 1) will be present in the recursive stack with the right node being 1 (Nth node), when we are coming out of the (N-1)th recursive state, list (1 -> 2 -> 3 -> 2 -> 1) will be present in the recursive stack with the right node being 2 ((N-1)th node), and so on…
So, when we are coming out of Nth recursive state, we will compare Nth node with 1st node,when we are coming out of (N-1)th recursive state, we will compare (N-1)th node with 2nd node,... when we are coming out of 2nd recursive state, we will compare 2nd node with (N-1)th node, when we are coming out of 1st recursive state, we will compare 1st node with Nth node.
If all these states return true, then the list is a palindrome.
Make a recursive function isPalindromeHelper(left, right) {pass left pointer by its reference}, this function checks whether the list between left and right is palindrome or not.
The idea is to divide the list into two halves from mid and reverse the second half. If both halves are equal then the given list is palindrome.