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) 的实现方式,快慢指针解法耗时更少:

  • 相关阅读:
    c#向某网址Post信息,并得到Cookies
    调度自动化系统及调度数据网安全分析探讨
    C#winform中怎么将程序最小化到系统托盘
    电力配网自动化解决方案
    .NET (c#) 模拟 Cookie
    调整数组顺序使奇数位于偶数前面
    在字符串中删除特定的字符
    在O(1)时间内删除链表结点
    单链表逆置
    从尾到头输出链表
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13185497.html
Copyright © 2011-2022 走看看