zoukankan      html  css  js  c++  java
  • Leetcode 234 回文链表,双指针与快慢指针

      双指针解法为两个指针,一个指向头元素,一个指向尾元素。两个指针逐步向中间靠拢并比较路过的元素:

     /**
         * @Author Niuxy
         * @Date 2020/6/23 8:22 下午
         * @Description 双指针
         */
        public final boolean isPalindrome(ListNode head) {
            if (head == null) {
                return true;
            }
            ListNode temp = head;
            //链表长度
            int length = 1;
            while (temp.next != null) {
                temp = temp.next;
                length++;
            }
            //双指针
            int before = 0;
            int after = length - 1;
            while (after > before) {
                if (getNode(head, before).val != getNode(head, after).val) {
                    return false;
                }
                before++;
                after--;
            }
            return true;
        }
    
        private final ListNode getNode(ListNode head, int index) {
            while (index != 0) {
                head = head.next;
                index--;
            }
            return head;
        }

      快慢指针解法为通过快慢指针找到中点,从中点断开链表。翻转其中一个链表后,比较两个链表是否相同:

     /**
         * @Author Niuxy
         * @Date 2020/6/23 8:22 下午
         * @Description 快慢指针翻转链表
         */
        public final boolean isPalindrome2(ListNode head) {
            if (head == null || head.next == null) {
                return true;
            }
            ListNode slow = head;
            ListNode fast = head.next.next;
            //快慢指针找中点
            while (fast != null && fast.next != null) {
                slow=slow.next;
                fast=fast.next.next;
            }
            ListNode seHead=revoleList(slow.next);
            slow.next=null;
            ListNode fiHead=head;
            while(fiHead!=null&&seHead!=null){
                if(fiHead.val!=seHead.val){
                    return false;
                }
                fiHead=fiHead.next;
                seHead=seHead.next;
            }
            return true;
        }
    
        //翻转链表
        private static ListNode revoleList(ListNode head) {
            if (head == null || head.next == null) {
                return null;
            }
            ListNode next = head.next.next;
            ListNode node = head.next;
            ListNode pre = head;
            head.next = null;
            while (node != null) {
                node.next = pre;
                if (next == null) {
                    return node;
                }
                pre = node;
                node = next;
                next = next.next;
            }
            return node;
        }

      两种解法均有空间复杂度为 O(1) 的实现方式,快慢指针解法耗时更少:

  • 相关阅读:
    [Swift]UIAlertController 以及 Swift 中的闭包和枚举
    递归算法
    [Javascript] Querying an Immutable.js Map()
    [Javascript] Modifying an Immutable.js Map()
    [Javascript] Creating an Immutable Object Graph with Immutable.js Map()
    [Javascript] Manage Application State with Immutable.js
    [Javascript] Using JSHint for Linting with Gulp
    [Angular + Unit] AngularJS Unit testing using Karma
    [AngularJS] Sane, scalable Angular apps are tricky, but not impossible.
    [Javascript] Array
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13185497.html
Copyright © 2011-2022 走看看