首页 > 其他分享 >Leetcode 回文链表

Leetcode 回文链表

时间:2024-03-27 20:46:37浏览次数:22  
标签:head ListNode cur next 链表 null Leetcode 回文

Day 12 第一题

用数组存储链表的数值,在检测是否是回文数组,数组长度不可变,所以用list
class Solution {
    public boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<>();

        while(head != null){
            list.add(head.val) ;
            head = head.next;
        }

        int n = list.size();
        for(int i=0;i<n;i++){
            if(list.get(i)!=list.get(n-1-i)){
                return false;
            }
        }

        return true;
    }
}
进阶:需要算法的时间复杂度\(\mathcal{O}(1)\),空间复杂度为\(\mathcal{O}(n)\)。使用快慢指针找到中间节点的位置,若数组为偶数,则快指针为空,慢指针指向\(n/2+1\);若数组数为奇数,则快指针不为空,慢指针指向\((n+1)/2\),难题在于反转后半部分链表;

class Solution {
    public boolean isPalindrome(ListNode head) {
        if(head==null || head.next==null){
            return true;
        }

        ListNode endOfFirst = endOfFirst(head);
        ListNode secondStart = reverse(endOfFirst.next);
        System.out.println(secondStart.val);

        while(head!=null && secondStart!=null){
            if(head.val != secondStart.val){
                return false;
            }
            
            head = head.next;
            secondStart = secondStart.next;

        }

        reverse(endOfFirst.next);
        return true;
        
    }

    private ListNode endOfFirst(ListNode head){
        ListNode fast = head;
        ListNode slow = head;

        while(fast.next.next!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public ListNode reverse(ListNode head){
        ListNode pre = null;
        ListNode cur = head;
        while(cur.next!=null){
            // 存储当前的下一个节点信息
            ListNode temp = cur.next;
            // 将链表箭头反转
            cur.next = pre;
            // 将pre随着链表反转向前推
            pre = cur;
            // 继续滑动节点向前
            cur = temp;
        }
        return cur;
    }
}

上述代码是过不了[1,0,0]的案例的,while(fast.next.next!=null && fast.next!=null){会报错。以及反转的最后一个节点没有反转,会导致第二部分始终只有一个节点。正确reverse如下:

    public ListNode reverse(ListNode head){
        ListNode pre = null;
        ListNode cur = head;
        while(cur!=null){
            // 存储当前的下一个节点信息
            ListNode temp = cur.next;
            // 将链表箭头反转
            cur.next = pre;
            // 将pre随着链表反转向前推
            pre = cur;
            // 继续滑动节点向前
            cur = temp;
        }
        return pre;
    }
补充:linkedList新增的首尾操作的特有方法

public E getFirst() 返回链表的第一个元素;

public E getLast() 返回链表的最后一个元素;

public E removeFirst() 删除链表的第一个元素;

public E removeLast() 删除链表的最后一个元素;

标签:head,ListNode,cur,next,链表,null,Leetcode,回文
From: https://www.cnblogs.com/xytang-mini-juan/p/18100178

相关文章

  • 链式栈回文字符串的判断(C++版)
    大家好我是大一新生,如果代码有啥错误和改进的地方可以评论哦,谢谢观念看;#include<iostream>#include<iomanip>usingnamespacestd;#defineok1#defineerror0#defineSelemtypechar#defineStatusint#defineMAXSIZE100typedefstructstack{//链式栈的结构  ......
  • 蓝桥杯试题 基础练习 特殊回文数
    问题描述123321是一个非常特殊的数,它从左边读和从右边读是一样的。输入一个正整数n,编程求所有这样的五位和六位十进制数,满足各位数字之和等于n。输入格式输入一行,包含一个正整数n。输出格式按从小到大的顺序输出满足条件的整数,每个整数占一行。样例......
  • leetcode-75
    给定一个包含红色、白色和蓝色、共n个元素的数组nums,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。我们使用整数0、1和2分别表示红色、白色和蓝色。必须在不使用库内置的sort函数的情况下解决这个问题。示例1:输入:nums=[2,0,2,1,1,......
  • LeetCodeHot100 链表 160. 相交链表 206. 反转链表 234. 回文链表 141. 环形链表
    160.相交链表https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=study-plan-v2&envId=top-100-likedpublicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){intlenA=0;intlenB=0;L......
  • leetcode.py
    importrequestsimportbase64importtimedefget_img_res():img=""start_time=time.time()withopen('img1.png','rb')asimage_file:#将图片内容转换为base64编码base64_image=base64.b64encode(image_file.rea......
  • 【蓝桥杯选拔赛真题48】C++九进制回文数 第十四届蓝桥杯青少年创意编程大赛 算法思维
    目录C++九进制回文数一、题目要求1、编程实现2、输入输出二、算法分析三、程序编写四、程序说明五、运行结果六、考点分析七、推荐资料C++九进制回文数第十四届蓝桥杯青少年创意编程大赛C++选拔赛真题一、题目要求1、编程实现提示信息:回文数:反向排列与原......
  • Leetcode 翻转二叉树
    Day11第三题目前ac最快的一道题/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(){}*TreeNode(intval){this.val=val;}*TreeNode(intval,TreeNo......
  • Leetcode 二叉树的最大深度
    Day11第二题深度优先搜索在计算当前二叉树的最大深度时,可以先递归计算出其左子树和右子树的最大深度,然后在\(\mathcal{O}(1)\)时间内计算出当前二叉树的最大深度。classSolution{publicintmaxDepth(TreeNoderoot){if(root==null){retur......
  • Leetcode 和为k的子数组
    Day11第一题#######解题思路:两层循环,用暴力法解决(从不同起始位置开始的子数组)classSolution{publicintsubarraySum(int[]nums,intk){//和为k的子数组的个数计数器countintcount=0;for(intj=0;j<nums.length;j++){......
  • 代码随想录算法训练营day35 | leetcode 860. 柠檬水找零、406. 根据身高重建队列、452
    目录题目链接:860.柠檬水找零-简单题目链接:406.根据身高重建队列-中等题目链接:452.用最少数量的箭引爆气球-中等题目链接:860.柠檬水找零-简单题目描述:在柠檬水摊上,每一杯柠檬水的售价为5美元。顾客排队购买你的产品,(按账单bills支付的顺序)一次购买一杯。每位顾客只买一......