-
-
Save kelly-us/0f3b3bd7a83e3b5c3f8b 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
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