234. 回文链表 Palindrome Linked List - 简单

这篇博客介绍了如何判断一个单链表是否为回文,提供了两种解题方法:递归法和快慢指针法。递归法通过从中间节点开始递归检查左右子链表的回文性质;快慢指针法则先找到链表中点,然后翻转后半部分链表进行比较。两种方法的时间复杂度均为O(N),但空间复杂度不同,递归法为O(N),快慢指针法为O(1)。
摘要由CSDN通过智能技术生成

回文链表(Palindrome Linked List)

题目描述

Given the head of a singly linked list, return true if it is a palindrome.
Example 1:

Input: head = [1,2,2,1]
Output: true

Example 2:

Input: head = [1,2]
Output: false

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 0 <= Node.val <= 9

Follow up: Could you do it in O(n) time and O(1) space?
Constraints:

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/palindrome-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解题方法

递归法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* left;
    bool recursivelyCheck(ListNode* right) {
        if(right == nullptr) return true;
        bool res = recursivelyCheck(right->next);
        res = res && (left->val == right->val);
        left = left->next;
        return res;
    }
    bool isPalindrome(ListNode* head) {
        left = head;
        return recursivelyCheck(head);
    }
};
  • 时间复杂度:O(N)。
  • 空间复杂度:O(N)。

快慢指针法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre = nullptr;
        ListNode* cur = head;
        ListNode* next;
        while(cur) {
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    bool isPalindrome(ListNode* head) {
        ListNode* slow = head;
        ListNode* fast = head;
        ListNode* p; //p为恢复链表结构作准备
        while(fast != nullptr && fast->next != nullptr) {
            p = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        if(fast != nullptr) {
            p = slow;
            slow = slow->next;
        }
        ListNode* left = head;
        ListNode* right = reverseList(slow);
        ListNode* q = right;
        bool flag = true;
        while(right != nullptr) {
            if(left->val != right->val) {
                flag = false;
            }
            else
                flag = true && flag;
            right = right->next;
            left = left->next;
        }
        p->next = reverseList(q);
        return flag;
    }
};
  • 时间复杂度:O(N)。
  • 空间复杂度:O(1)。
  • 由于翻转链表后半部分破坏了链表的原有结构,故设置p,q来还原链表结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值