Skip to content

Instantly share code, notes, and snippets.

@Roger7410
Created October 15, 2016 01:32
Show Gist options
  • Select an option

  • Save Roger7410/393b096632bb5b8682a7431e77b66ea5 to your computer and use it in GitHub Desktop.

Select an option

Save Roger7410/393b096632bb5b8682a7431e77b66ea5 to your computer and use it in GitHub Desktop.
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