/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

 /*
 Use runner technique while reverse the slow runner part.
 Then verfiy the value of slow.val and pre.val
 */
public class Solution {
    public boolean isPalindrome(ListNode head) {
        if (head == null) {
            return true;
        }

        ListNode slow = head;
        ListNode fast = head;
        ListNode pre = null;

        while (fast!=null && fast.next!=null) {
            fast = fast.next.next;
            ListNode temp = slow.next;
            slow.next = pre;
            pre = slow;
            slow = temp;
        }

        if (fast!=null) {
            slow = slow.next;
        }

        while (slow!=null) {
            System.out.println(slow.val);
            if (slow.val!=pre.val) {
                return false;
            }
            slow = slow.next;
            pre = pre.next;
        }

        return true;

    }
}

results matching ""

    No results matching ""