【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));
    }
}
相关推荐
web_155342746562 分钟前
性能巅峰对决:Rust vs C++ —— 速度、安全与权衡的艺术
c++·算法·rust
计算机小白一个7 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
万事可爱^7 小时前
HDBSCAN:密度自适应的层次聚类算法解析与实践
算法·机器学习·数据挖掘·聚类·hdbscan
大数据追光猿9 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Dream it possible!9 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
夏末秋也凉9 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
南宫生9 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
柠石榴9 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
Leuanghing9 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
qy发大财9 小时前
加油站(力扣134)
算法·leetcode·职场和发展