zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    PDA智能程序访问WebService,报告“未能建立与网络的连接”
    VS2008中开发智能设备程序的一些总结收藏
    Error: The INF file contains Unicode characters that could not be converted correctly
    在vs2008工程中制作cab包
    linux专题三之如何悄悄破解root密码(以redhat7.2x64为例)
    linux专题一之文件描述符、重定向、管道符、tee命令
    linux的计划
    如何安装RHEL7.2x64 即红帽7.2虚拟机?
    快速排序及查找第K个大的数。
    来来来,做道题,一起防老年痴呆
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9704553.html
Copyright © 2011-2022 走看看