Last active
April 22, 2018 14:18
-
-
Save Kishy-nivas/533b0a509faa0885aaa4f922170fdc1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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