【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));
    }
}
相关推荐
重生之我是Java开发战士9 分钟前
【广度优先搜索】多源BFS:矩阵,飞地的数量,地图中的最高点,地图分析
数据结构·算法·矩阵·广度优先
sali-tec17 分钟前
C# 基于OpenCv的视觉工作流-章43-轮廓匹配
图像处理·人工智能·opencv·算法·计算机视觉
Coovally AI模型快速验证27 分钟前
检测+跟踪一体化!4.39M参数、8.3W功耗,轻量化模型让无人机在露天矿实时巡检
算法·yolo·无人机·智能巡检·智慧矿山
玛卡巴卡ldf30 分钟前
【LeetCode 手撕算法】(矩阵)73-矩阵置零、54-螺旋矩阵(贪吃蛇)、48-旋转图像
java·数据结构·算法·leetcode·力扣
C^h30 分钟前
RTthread中的内存池理解
linux·数据库·c++·算法·嵌入式
深藏功yu名30 分钟前
Day25(高阶篇):RAG检索与重排序算法精研|从原理到参数调优,彻底攻克检索瓶颈
人工智能·算法·ai·自然语言处理·排序算法·agent
郝学胜-神的一滴35 分钟前
深入解析:生成器在UserList中的应用与Python可迭代对象实现原理
开发语言·python·程序人生·算法
雪木木35 分钟前
刷题:力扣热题100--滑动窗口(Day03)
算法·leetcode
Yzzz-F38 分钟前
Problem - 2157D - Codeforces
算法
颜酱41 分钟前
回溯算法实战练习(2)
javascript·后端·算法