Hi there ,
i was solving a problem to find whether a given singly LL is palindrome or not .
after the solving the problem by reversing the linked list from middle , i came across the recursive solution for this , but in that approach they are using a Node type Class level variable
class LinkedList
{
Node head; // head of list
Node left;
boolean isPalindromeUtil(Node right)
{
left = head;
/* stop recursion when right becomes NULL */
if (right == null)
return true;
/* If sub-list is not palindrome then no need to
check for current left and right, return false */
boolean isp = isPalindromeUtil(right.next);
if (isp == false)
return false;
/* Check values at current left and right */
boolean isp1 = (right.data == (left).data);
/* Move left to next node */
left = left.next;
return isp1;
}
}
Here “head” variable is global (class level), but as per my understanding using global variables are not recommended .
can you please provide a clear explanation for this .