Palindrome list [LeetCode list + O(1) spatial complexity]

Please judge whether a linked list is palindrome linked list.

Example 1:

Input: 1->2
 Output: false

Example 2:

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

Advance:
Can you use O(n) time complexity and O(1) space complexity to solve this problem?

If there is no limitation of space complexity, we can use the stack to complete the operation, use the fast and slow pointer, put the first half into the stack, and then judge the second half in turn.

If under the limitation of O(1) space complexity, we can only operate in the linked list, so we can reverse the linked list, use the fast and slow pointer to reverse the previous part of the linked list, then we should judge whether the length of the linked list is odd or even, for example:

For the difference between 1 - > 2 - > 2 - > 1 and 1 - > 2 - > 3 - > 2 - > 1, we use the fast and slow pointer to find the position of (n+1)/2 for inversion, so the first one is 2 - > 1,2 - > 1, and the second one is 1 - > 2 - > 3,2 - > 1, so we need to judge the parity, with comments in the specific code.

public class Main {
    public static void main(String[] args) {
        ListNode h=new ListNode(1);
        h.next=new ListNode(2);
        h.next.next=new ListNode(2);
        h.next.next.next=new ListNode(1);
        System.out.println(isPalindrome(h));
    }

    public static boolean isPalindrome(ListNode head) {
        //First find the intermediate node, then reverse the previous part, and then compare the two linked lists
        //1. Find the intermediate nodes, one fast, one slow
        ListNode fast=head;
        ListNode slow=head;
        if(fast==null || fast.next==null){//Returns true directly if there is no element or only one element
            return true;
        }

        while(fast.next!=null && fast.next.next!=null){//Find the pointer position of (n+1)/2. N is the number of elements. For example, if there are three elements, then
            fast=fast.next.next;                       //slow is at 2. If it's 4, it's still at 2,
            slow=slow.next;                            //Because palindrome is 1211221, we need to consider odd digit or even digit
        }
        int flag=0;//Flag bit, judge whether the linked list is odd or even. If it is 1, it means it is odd
        if(fast.next==null){
            flag=1;
        }

        ListNode head2=slow.next;//Start pointer of the second link list
        //Inverted list
        ListNode pre=null;
        ListNode now=head;
        ListNode next=head;
        while(next!=head2){
            next=now.next;
            now.next=pre;

            pre=now;
            now=next;
        }

        if(flag==1){//If it's an odd bit like 121, the middle 2 doesn't participate in the judgment, just like the original 1 - > 2 - > 1. After reversing, one segment is 2 - > 1, and the other segment is 1,
            pre=pre.next;
        }

        while(head2!=null){
            if(head2.val!=pre.val){
                return false;
            }
            head2=head2.next;
            pre=pre.next;
        }
        return true;
    }
    static public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}

 

Posted by ojeffery on Mon, 30 Dec 2019 20:11:10 -0800