Skip to content

Instantly share code, notes, and snippets.

@kelly-us
Created April 13, 2015 14:12
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 kelly-us/0f3b3bd7a83e3b5c3f8b to your computer and use it in GitHub Desktop.
Save kelly-us/0f3b3bd7a83e3b5c3f8b to your computer and use it in GitHub Desktop.
public boolean isPalindrome(ListNode head){
if(head == null || head.next == null) return true;
Stack<Integer> s = new Stack<Integer>();
ListNode slow = head;
ListNode fast = head;
while(fast.next != null){
s.push(slow.data);
slow = slow.next;
fast = fast.next.next;
}
if(fast != null){
slow = slow.next;
}
while(slow != null){
int top = s.pop().intValue();
if(top != slow.data) 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