【Hot100】LeetCode—234. 回文链表

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐234. 回文链表------题解思路](#⭐234. 回文链表——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

快慢指针+链表拆分+反转链表

思路
①将链表拆分前后两个部分------>找拆分点②反转后面部分③根据反转结果,同时利用两个指针遍历

  • ① 找拆分点:快慢指针
    • 利用快慢指针,满指针的 next 就是 后半部分的头指针
  • ② 反转链表
    • 递归反转后半部分
  • ③ 遍历判断
    • 依次同时移动两个指针判断

2- 实现

⭐234. 回文链表------题解思路

java 复制代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode endA = endOfA(head);

        boolean res = true;
        ListNode headB = reverseL(endA.next);

        ListNode curA = head;
        ListNode curB = headB;

        while(res && curB!=null){
            if(curA.val != curB.val){
                res = false;
            }
            curA = curA.next;
            curB = curB.next;
        }
        // 恢复 B 
        reverseL(headB);
        return res;
    }

    public ListNode endOfA(ListNode head){
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next!=null && fast.next.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }


    public ListNode reverseL(ListNode head){
        if(head==null || head.next==null){
            return head;
        }
        ListNode cur = reverseL(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }
}

3- ACM 实现

java 复制代码
public class isPalindrome {

    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }


    public static boolean isP(ListNode head){
        ListNode endA = endOfA(head);
        // 反转
        ListNode headB = reverseL(endA.next);

        ListNode curA = head;
        ListNode curB = headB;
        boolean res = true;
        while(curB!=null){
            if(curA.val != curB.val){
                res = false;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return res;
    }

    private static ListNode endOfA(ListNode head){
        ListNode slow = head;
        ListNode fast = head;
        while(fast.next!=null && fast.next.next!=null){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    private static ListNode reverseL(ListNode head){
        if(head == null || head.next==null){
            return head;
        }
        ListNode cur = reverseL(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入链表长度");
        int n = sc.nextInt();
        ListNode head = null,tail=null;
        for(int i = 0 ; i < n;i++){
            ListNode nowNode  = new ListNode(sc.nextInt());
            if(head==null){
                head = nowNode;
                tail = nowNode;
            }else{
                tail.next = nowNode;
                tail = nowNode;
            }
        }
        System.out.println("结果是"+isP(head));
    }
}
相关推荐
^^为欢几何^^1 分钟前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
豆浩宇1 分钟前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
浅念同学17 分钟前
算法.图论-并查集上
java·算法·图论
何不遗憾呢26 分钟前
每日刷题(算法)
算法
立志成为coding大牛的菜鸟.30 分钟前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞30 分钟前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先
liangbm336 分钟前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
潮汐退涨月冷风霜41 分钟前
机器学习之非监督学习(四)K-means 聚类算法
学习·算法·机器学习
B站计算机毕业设计超人1 小时前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~1 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn