Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Last active April 22, 2018 14:18
Show Gist options
  • Save Kishy-nivas/533b0a509faa0885aaa4f922170fdc1b to your computer and use it in GitHub Desktop.
Save Kishy-nivas/533b0a509faa0885aaa4f922170fdc1b to your computer and use it in GitHub Desktop.
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
/* Structure of class Node is
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}*/
class GfG
{
boolean isPalindrome(Node head)
{
if(head == null|| head.next == null)
return true;
Node slow = head;
Node fast = head;
Stack<Integer> st = new Stack<Integer>();
while(fast !=null && fast.next != null){
st.push(slow.data);
fast =fast.next.next;
slow = slow.next;
}
if(fast !=null ) // odd number
slow = slow.next;
while(slow != null){
if(slow.data != st.pop())
return false;
slow = slow.next;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment