Skip to content

Instantly share code, notes, and snippets.

@JoyceeLee
Created July 2, 2014 17:15
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 JoyceeLee/23e37c323192dd6f3405 to your computer and use it in GitHub Desktop.
Save JoyceeLee/23e37c323192dd6f3405 to your computer and use it in GitHub Desktop.
/*2.7 Implement a function to check if a linked list is a palindrome.*/
public class Solution {
public boolean isPalindrome(ListNode head){
ListNode fast = head;
ListNode slow = head;
Stack<Integer> st = new Stack<Integer>();
while(fast!=null && fast.next!=null) {
st.push(slow.val);
fast = fast.next.next;
slow = slow.next;
}
if(fast!=null) {
slow = slow.next;
}
while(slow!=null) {
if(slow.val != st.pop()) {
return false;
}
slow = slow.next;
}
return true;
}
}
@jason51122
Copy link

  1. Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment