Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created June 30, 2014 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hilda8519/55ba79b63e25ea971166 to your computer and use it in GitHub Desktop.
Save hilda8519/55ba79b63e25ea971166 to your computer and use it in GitHub Desktop.
import java.util.Stack;
public class isPalindrome {
public static boolean isPalindrome(ListNode head){
ListNode fast=head;
ListNode slow=head;
Stack<Integer> stack=new Stack<Integer>();
while(fast!=null&&fast.next!=null){
stack.push(slow.data);
slow=slow.next;
fast=fast.next.next;
}
if(fast!=null){
slow=slow.next;
}
while(slow!=null){
int top=stack.pop().intValue();
if(top!=slow.data){
return false;
}
slow=slow.next;
}
return false;
}
class ListNode{
public Object data;
int val;
ListNode next;
ListNode(int i){
val=i;
next=null;
}
public static void main (String[] args){
String s="MON";
System.out.println(isPalindrome(s));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment