234. Palindrome Linked List

Question

Given a singly linked list, determine if it is a palindrome.

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Thinking:

  • Method 1:反转右半部分的链表,头尾相互比较。
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) return true;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode slow = dummy;
        ListNode fast = dummy;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode pre = null;
        ListNode next = null;
        ListNode cur = slow.next;
        slow.next = null;
        do{
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }while(next != null);
        while(pre != null && head != null){
            if(pre.val != head.val) return false;
            pre = pre.next;
            head = head.next;
        }
        return true;
    }
}

二刷

  1. Method 1: I first save the list as array and then checked the palindrome of the array.
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
     public boolean isPalindrome(ListNode head) {
         ListNode cur = head;
         List<Integer> temp = new LinkedList<>();
         while(cur != null){
             temp.add(cur.val);
             cur = cur.next;
         }
         Integer[] arr = new Integer[temp.size()];
         for(int i = 0; i < temp.size(); i++){
             arr[i] = (Integer)temp.get(i);
         }
         int start = 0, end = arr.length - 1;
         while(start < end){
             if(!arr[start++].equals(arr[end--])){
                 return false;
             }
         }
         return true;
     }
    }
    
  2. Method 2, I reversed the first half of the list, and compare the first half and second half.
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
     public boolean isPalindrome(ListNode head) {
         if(head == null) return true;
         ListNode dummy = new ListNode(0);;
         dummy.next = head;
         ListNode slow = dummy;
         ListNode fast = dummy;
         while(fast != null && fast.next != null){
             slow = slow.next;
             fast = fast.next.next;
         }
         ListNode temp = slow.next;
         slow.next = null;
         ListNode pre = null;
         ListNode cur = head;
         ListNode next = null;
         while(cur != null){
             next = cur.next;
             cur.next = pre;
             pre = cur;
             cur = next;
         }
         cur = fast == null ? pre.next : pre;
         while(cur != null && temp != null){
             if(cur.val != temp.val) return false;
             cur = cur.next;
             temp = temp.next;
         }
         return true;
     }
    }