zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    windows下 php-cgi.exe 0xc000007b 错误 阿星小栈
    call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败 阿星小栈
    PHP json_encode返回的json前端获取时出现unicode转码和反斜杠导致无法解析的解决办法
    Warring:POST Content-Length of 625523488 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 阿星小栈
    PHP数组分割成新数组 阿星小栈
    Laravel ajax请求419错误及解决办法(CSRF验证) 阿星小栈
    MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded... 阿星小栈
    Laravel框架发送邮件 阿星小栈
    PHP 导出Excel三种方式 阿星小栈
    mysql命令 出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5002340.html
Copyright © 2011-2022 走看看