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;
    }
}
相关推荐
evans在进步8 分钟前
LeetCode 198:打家劫舍——Java 动态规划详解
java·leetcode·动态规划
晚风叙码31 分钟前
C++哈希表实现:开放定址法和链地址法 (哈希桶)
数据结构·c++·哈希算法·散列表
imaol132 分钟前
哈希表--数据结构
数据结构·散列表
JL151 小时前
Java并发编程面试全攻略-从synchronized到AQS底层原理
java·开发语言·面试·并发编程
ZC跨境爬虫1 小时前
LeetCode 119. 杨辉三角 II(原地更新优化详解 + Java Python 实现)
java·python·leetcode
比奇堡裤头村1 小时前
统计学习方法——支持向量机
算法·支持向量机·学习方法
程序员爱钓鱼1 小时前
Go 编程实战:数组 Array——固定长度的数据集合
后端·面试·go
ShineWinsu9 小时前
对于C++:auto_ptr、unique_ptr、shared_ptr的模拟实现
c++·面试·笔试·智能指针·unique_ptr·shared_ptr·auto_ptr
黄敬峰12 小时前
一文搞懂 NestJS 后端框架:工厂模式、模块化与装饰器
面试