Created
October 15, 2016 01:32
-
-
Save Roger7410/393b096632bb5b8682a7431e77b66ea5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class Solution { | |
| public boolean isPalindrome(ListNode head) { | |
| if(head == null || head.next == null) return true; | |
| ListNode slow = head, fast = head; | |
| ListNode cur = head, rev = null; | |
| //Finding mid point with fast slow, while also reversing first half | |
| while(fast != null && fast.next != null){ | |
| slow = slow.next; | |
| fast = fast.next.next; | |
| cur.next = rev; | |
| rev = cur; | |
| cur = slow; | |
| } | |
| // Watch out for odd palindrome | |
| if(fast!=null) slow = slow.next; | |
| // Check palindrome | |
| while(rev != null ){ | |
| if(rev.val != slow.val) return false; | |
| rev = rev.next; | |
| slow = slow.next; | |
| } | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment