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

Leetcode 234.回文链表

时间:2024-08-14 14:23:23浏览次数:10  
标签:head ListNode next 链表 234 return null Leetcode

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

示例 1:

输入:head = [1,2,2,1]
输出:true

示例 2:

输入:head = [1,2]
输出:false

提示:

  • 链表中节点数目在范围[1, 105] 内
  • 0 <= Node.val <= 9

进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

方法一:常规思路,将链表反转,将反转后的链表与原来的链表中的值想比较

class Solution {
    public static boolean isPalindrome(ListNode head) {
        ListNode newHead = copyList(head);
        ListNode reverseNode = reverse(head);//反
        //遍历节点
        while(newHead != null){
            if(newHead.val != reverseNode.val) return false;
            newHead =newHead.next;
            reverseNode = reverseNode.next;
        }
        return true;
    }
    //反转链表:从后往前递归
    private static ListNode reverse(ListNode head){
        if(head == null) return null;
        //递归结束条件
        if(head.next == null) return head;
        ListNode last = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }
    
    public static ListNode copyList(ListNode head) {
        if (head == null) return null;
        ListNode newHead = new ListNode(head.val); // 创建新链表的头节点
        ListNode curr = head.next; // 用于遍历原始链表
        ListNode newCurr = newHead; // 用于构建新链表

        while (curr != null) {
            newCurr.next = new ListNode(curr.val); // 复制当前节点并添加到新链表中
            curr = curr.next; // 移动到原始链表的下一个节点
            newCurr = newCurr.next; // 移动到新链表的下一个节点
        }

        return newHead; // 返回新链表的头节点
    }
}

方法二:将值复制到数组中后用双指针法

class Solution {
    public boolean isPalindrome(ListNode head) {
        List<Integer> vals = new ArrayList<Integer>();

        // 将链表的值复制到数组中
        ListNode currentNode = head;
        while (currentNode != null) {
            vals.add(currentNode.val);
            currentNode = currentNode.next;
        }

        // 使用双指针判断是否回文
        int front = 0;
        int back = vals.size() - 1;
        while (front < back) {
            if (!vals.get(front).equals(vals.get(back))) {
                return false;
            }
            front++;
            back--;
        }
        return true;
    }
}

方法三:递归

class Solution {
    private ListNode frontPointer;

    private boolean recursivelyCheck(ListNode currentNode) {
        if (currentNode != null) {
            if (!recursivelyCheck(currentNode.next)) {
                return false;
            }
            if (currentNode.val != frontPointer.val) {
                return false;
            }
            frontPointer = frontPointer.next;
        }
        return true;
    }

    public boolean isPalindrome(ListNode head) {
        frontPointer = head;
        return recursivelyCheck(head);
    }
}

方法四:快慢指针

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

        // 找到前半部分链表的尾节点并反转后半部分链表
        ListNode firstHalfEnd = endOfFirstHalf(head);
        ListNode secondHalfStart = reverseList(firstHalfEnd.next);

        // 判断是否回文
        ListNode p1 = head;
        ListNode p2 = secondHalfStart;
        boolean result = true;
        while (result && p2 != null) {
            if (p1.val != p2.val) {
                result = false;
            }
            p1 = p1.next;
            p2 = p2.next;
        }        

        // 还原链表并返回结果
        firstHalfEnd.next = reverseList(secondHalfStart);
        return result;
    }

    private ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    private ListNode endOfFirstHalf(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

标签:head,ListNode,next,链表,234,return,null,Leetcode
From: https://blog.csdn.net/m0_74051652/article/details/141190107

相关文章

  • 每日一题:Leetcode-662 二叉树最大宽度
    力扣题目解题思路java代码力扣题目:给你一棵二叉树的根节点 root ,返回树的 最大宽度 。树的 最大宽度 是所有层中最大的 宽度 。每一层的 宽度 被定义为该层最左和最右的非空节点(即,两个端点)之间的长度。将这个二叉树视作与满二叉树结构相同,两端点间会出现一些......
  • 对链表进行排序
    1publicclassSortLinkedList{//方法:对链表进行排序publicstaticListNodesortList(ListNodehead){//如果链表为空或只有一个节点,直接返回if(head==null||head.next==null){returnhead;}//使用......
  • 合并两个有序链表
    1、publicclassMergeTwoSortedLists{//方法:合并两个有序链表publicstaticListNodemergeTwoLists(ListNodel1,ListNodel2){//创建一个虚拟节点作为合并后链表的头节点ListNodedummy=newListNode(0);ListNodecurrent=du......
  • 数据结构之链表超详解(1)
     一人我饮酒醉醉把佳人成双对两眼是独相随只求他日能双归娇女我轻扶琴燕嬉我紫竹林痴情红颜心甘情愿千里把你寻说红颜我痴情笑曲动我琴声妙轻狂高傲懵懂无知只怪太年少弃江山我忘天下斩断情丝无牵挂千古留名传佳话我两年征战已白发一生征战何人陪谁是谁非谁相随戎马一......
  • LeetCode 1383. Maximum Performance of a Team
    原题链接在这里:https://leetcode.com/problems/maximum-performance-of-a-team/description/题目:Youaregiventwointegers n and k andtwointegerarrays speed and efficiency bothoflength n.Thereare n engineersnumberedfrom 1 to n. speed[i] a......
  • Leetcode JAVA刷刷站(20)有效的括号
    一、题目概述二、思路方向     在Java中,要判断一个仅包含括号('(',')','{','}','[',']')的字符串是否有效,你可以使用栈(Stack)数据结构来实现。栈是一种后进先出(LIFO,LastInFirstOut)的数据结构,非常适合用来处理这类问题。以下是具体的实现步骤和代码示例:创......
  • LeetCode 1359. Count All Valid Pickup and Delivery Options
    原题链接在这里:https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/description/题目:Given n orders,eachorderconsistsofapickupandadeliveryservice.Countallvalidpickup/deliverypossiblesequencessuchthatdelivery(i)isalw......
  • 内核链表常用宏——container_of()
    定义#definelist_entry(ptr,type,member)\ container_of(ptr,type,member)#defineoffsetof(TYPE,MEMBER)((size_t)&((TYPE*)0)->MEMBER)#definecontainer_of(ptr,type,member)({ \ consttypeof(((type*)0)->member)*__mptr=(ptr); ......
  • leetcode面试经典150题-122. 买卖股票的最佳时机 II
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/description/?envType=study-plan-v2&envId=top-interview-150 gopackageleetcode150import"testing"funcTestMaxProfit2(t*testing.T){prices:=[]int{7,1,5,3,6,4}......
  • leetcode面试经典150题- 121. 买卖股票的最佳时机
    https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/?envType=study-plan-v2&envId=top-interview-150gopackageleetcode150import("testing")funcTestMaxProfit(t*testing.T){prices:=[]int{2,1,2,0,1}......