• 234. Palindrome Linked List


    题目:

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

    Follow up:
    Could you do it in O(n) time and O(1) space?

    链接: http://leetcode.com/problems/palindrome-linked-list/

    题解:

    判断链表是否是Palindrome。 我们分三步解,先用快慢指针找中点,接下来reverse中点及中点后部,最后逐节点对比值。

    Time Complexity - O(n), Space Complexity - O(1)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if(head == null || head.next == null)
                return true;
            ListNode mid = findMid(head);
            ListNode tail = reverse(mid);
            mid.next = null;
            
            while(head != null && tail != null) {
                if(head.val != tail.val)
                    return false;
                else {
                    head = head.next;
                    tail = tail.next;
                }
            }
            
            return true;
        }
        
        private ListNode findMid(ListNode head) {               // find mid node of list
            if(head == null || head.next == null)
                return head;
            ListNode slow = head, fast = head;
            
            while(fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            
            return slow;
        }
        
        private ListNode reverse(ListNode head) {           // reverse listnode
            if(head == null || head.next == null)
                return head;
            ListNode dummy = new ListNode(-1);
            while(head != null) {
                ListNode tmp = head.next;
                head.next = dummy.next;
                dummy.next = head;
                head = tmp;
            }
            return dummy.next;
        }
    }

    二刷:

    和一刷一样,先快慢指针找重点,然后reverse后半部分,接下来遍历两个head逐个对比节点的值。最后返回true.

    Java

    Time Complexity - O(n), Space Complexity - O(1)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if (head == null || head.next == null) {
                return true;
            }
            ListNode mid = findMid(head);
            ListNode reversedMid = reverse(mid);
            ListNode node = head;
            while (node != null && reversedMid != null) {
                if (node.val != reversedMid.val) {
                    return false;
                }
                node = node.next;
                reversedMid = reversedMid.next;
            }
            return true;
        }
        
        private ListNode findMid(ListNode head) {
            ListNode fast = head, slow = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
        
        
        private ListNode reverse(ListNode head) {
            ListNode dummy = new ListNode(-1);
            ListNode next = null;
            while (head != null) {
                next = head.next;
                head.next = dummy.next;
                dummy.next = head;
                head = next;
            }
            return dummy.next;
        }
        
        
        
    }

    三刷:

    跟二刷一样

    Java:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if (head == null || head.next == null) return true;
            ListNode mid = findMid(head);
            ListNode reversedMid = reverse(mid);
            ListNode node = head;
            while (node != null && reversedMid != null) {
                if (node.val != reversedMid.val) return false;
                node = node.next;
                reversedMid = reversedMid.next;
            }
            return true;
        }
        
        private ListNode findMid(ListNode head) {
            ListNode fast = head, slow = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
        
        
        private ListNode reverse(ListNode head) {
            ListNode dummy = new ListNode(-1);
            ListNode next = null;
            while (head != null) {
                next = head.next;
                head.next = dummy.next;
                dummy.next = head;
                head = next;
            }
            return dummy.next;
        }
        
        
        
    }

    Update:

    这样写确实会破坏原来链表的结构。而且反转后半部分的时候是否可以可以算作O(1) space也值得商榷

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if (head == null || head.next == null) return true;
            ListNode mid = findMid(head);
            ListNode tailReversed = reverse(mid);
            while (tailReversed != null && head != null) {
                if (tailReversed.val != head.val) return false;
                tailReversed = tailReversed.next;
                head = head.next;
            }
            return true;
        }
        
        private ListNode findMid(ListNode head) {
            ListNode fast = head, slow = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
        
        private ListNode reverse(ListNode head) {
            ListNode dummy = new ListNode(-1);
            ListNode tmp = null;
            while (head != null) {
                tmp = head.next;
                head.next = dummy.next;
                dummy.next = head;
                head = tmp;
            }
            return dummy.next;
        }
    }

    Reference:

    https://leetcode.com/discuss/44751/11-lines-12-with-restore-o-n-time-o-1-space

  • 相关阅读:
    数据库语句
    Java类和数据结构中常用的方法
    java知识点
    JVM相关知识
    Java实现三种常用的查找算法(顺序查查找,折半查找,二叉排序树查找)
    SharedPreference作用及数据操作模式
    Java实现7种常见的排序算法
    钱多多软件制作03
    钱多多软件制作02
    钱多多软件制作01
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5002340.html
Copyright © 2020-2023  润新知