• leetcode234


    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
    Follow up:
    Could you do it in O(n) time and O(1) space?
     
    1.stack。O(n), O(n)
    快慢指针找出中点,在找中点的过程中把前半段压入栈。接下来对比中点后半段的内容和stack pop出来的内容是否一致,出现不一致就是false,遍历完为true。
    2.半段链表反转。O(n), O(1)。for follow up。
    快慢指针找出中点,把中点后半段链表反转,接着从head和中点开始并行往前,一个个对比。 
     
    实现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) {
            Stack<Integer> stack = new Stack<>();
            ListNode fast = head, slow = head;
            // 1-2-3-4    32  null3
            // 1-2-3      32
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                stack.push(slow.val);
                slow = slow.next;
            }
            if (fast != null) {
                slow = slow.next;
            }
            while (!stack.isEmpty()) {
                if (stack.peek() != slow.val) {
                    return false;
                }
                stack.pop();
                slow = slow.next;
            }
            return true;
        }
    }

    实现2:

    /**
     * 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 mid = findMiddle(head);
            mid = reverseList(mid);
            while (head != null && mid != null) {
                if (head.val != mid.val) {
                    return false;
                }
                head = head.next;
                mid = mid.next;
            }
            return true;
        }
        
        private ListNode findMiddle(ListNode head) {
            ListNode fast = head, slow = head;
            // 1-2-3-4    32  null3
            // 1-2-3      32
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            // return the first node after the midpoint. 1234 -> 3, 12345 -> 4
            if (fast != null) {
                slow = slow.next;
            }
            return slow;
        }
        
        private ListNode reverseList(ListNode head) {
            ListNode prev = null;
            while (head != null) {
                ListNode temp = head.next;
                head.next = prev;
                prev = head;
                head = temp;
            }
            return prev;
        }
    }
  • 相关阅读:
    php 23种设计模型
    PhpStrom 常用的插件
    PhpStrom 好用的代码小地图插件
    php 23种设计模型
    phpstrom怎样显示类的方法或函数列表
    超好用的谷歌助手插件
    PhpStrom 好用的翻译插件
    php 23种设计模型
    【java】匿名对象
    【java】类的初识
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9704553.html
Copyright © 2020-2023  润新知