A.每日一题:234. 回文链表

题目链接:234. 回文链表(简单)

算法原理:

本题与 A.每日一题:876. 链表的中间结点+2130. 链表最大孪生和 中的 2130题基本一模一样,只是在部分地方有修改,这里就不过多赘述了,大家可以自行参考上面这篇博客~~

解法一:双指针+顺序表

7ms击败35.92%

时间复杂度O(N)

解法二:递归

17ms击败6.07%

时间复杂度O(N)

解法三:迭代

5ms击败59.18%

时间复杂度O(N)

Java代码:

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 {
    //234. 回文链表
    //解法一:双指针+顺序表
    public boolean isPalindrome(ListNode head) {
        List<Integer> list=new ArrayList<>();
        ListNode cur=head;
        while(cur!=null){
            list.add(cur.val);
            cur=cur.next;
        }
        int left=0,right=list.size()-1;
        while(left<right){
            if(list.get(left)!=list.get(right)) return false;
            left++;right--;
        }
        return true;
    }
}
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 {
    //234. 回文链表
    //解法二:递归
    private ListNode left;
    public boolean isPalindrome(ListNode head) {
        left=head;
        return dfs(head);
    }
    private boolean dfs(ListNode right){
        //递:先把right移到链表末尾
        if(right.next!=null&&!dfs(right.next)) return false;
        //归:从右到左遍历链表
        if(left.val!=right.val) return false;
        //left 往右走
        left=left.next;
        //归:right会往左走
        return true;
    }

}
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 {
    //234. 回文链表
    //解法三:递归
    public boolean isPalindrome(ListNode head) {
        ListNode mid=middleNode(head);
        ListNode head2=reverseList(mid);
        while(head2!=null){
            if(head.val!=head2.val) return false;
            head=head.next;
            head2=head2.next;
        }
        return true;
    }
    //876.链表的中间结点
    private ListNode middleNode(ListNode head){
        ListNode slow=head;
        ListNode fast=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;
    }
    //206.反转链表
    public ListNode reverseList(ListNode head) {
        ListNode pre=null;
        ListNode cur=head;
        while(cur!=null){
            ListNode nxt=cur.next;
            cur.next=pre;
            pre=cur;
            cur=nxt;
        }
        return pre;
    }
}
相关推荐
清泓y1 天前
UE移动开发技术面试题
android·面试·ue5·ue4·游戏程序
刘沅1 天前
Redis的哨兵机制
后端·面试
凉凉的知识库1 天前
什么是 HTTP Keep-Alive?一文讲清连接复用机制
网络协议·http·面试
清泓y1 天前
UE基础知识与引擎架构面试题
面试·架构·ue5·ue4·游戏程序
玖玥拾1 天前
LeetCode 13 罗马数字转整数
笔记·算法·leetcode
触底反弹1 天前
💡 React 父子组件通信:一个进度条教会我的 5 件事
前端·react.js·面试
起个破名想半天了1 天前
算法与数据结构之Kruskal算法
数据结构
艾莉丝努力练剑1 天前
【MYSQL】MYSQL学习的一大重点:基本查询(下)
android·数据库·学习·mysql·面试·八股文
不会就选b1 天前
算法日常・每日刷题--<链表>3
数据结构·算法·链表
测试界的世清1 天前
各大厂软件测试面试题+答案纯干货
面试·职场和发展