Created
July 2, 2014 17:15
-
-
Save JoyceeLee/23e37c323192dd6f3405 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
/*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
commented
Jul 3, 2014
- Cool!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment