Created
June 30, 2014 00:24
-
-
Save hilda8519/55ba79b63e25ea971166 to your computer and use it in GitHub Desktop.
This file contains 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
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