【LeetCode每日一题】——剑指 Offer II 027.回文链表

一【题目类别】

二【题目难度】

  • 简单

三【题目编号】

  • 剑指 Offer II 027.回文链表

四【题目描述】

  • 给定一个链表的 头节点 head ,请判断其是否为回文链表。
  • 如果一个链表是回文,那么链表节点序列从前往后看和从后往前看是相同的。

五【题目示例】

  • 示例 1:

    • 在这里插入图片描述
    • 输入: head = [1,2,3,3,2,1]
    • 输出: true
  • 示例 2:

    • 在这里插入图片描述
    • 输入: head = [1,2]
    • 输出: false

六【题目提示】

  • 链表 L L L 的长度范围为 [ 1 , 1 0 5 ] [1, 10^5] [1,105]
  • 0 < = n o d e . v a l < = 9 0 <= node.val <= 9 0<=node.val<=9

七【题目进阶】

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

八【题目注意】

九【解题思路】

  • 本题比较简单,直接使用快慢指针找到链表的中间位置
  • 然后将链表的前半部分入栈
  • 最后将链表的后半部分和栈中的元素比较
  • 如果比较后栈为空,说明是回文链表,否则不是
  • 最后返回结果即可

十【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n) n n n为链表的长度
  • 空间复杂度: O ( n ) O(n) O(n) n n n为链表的长度

十一【代码实现】

  1. Java语言版
class Solution {
    
    
    public boolean isPalindrome(ListNode head) {
    
    
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
    
    
            slow = slow.next;
            fast = fast.next.next;
        }
        Stack<ListNode> stack = new Stack<>();
        while(head != slow){
    
    
            stack.push(head);
            head = head.next;
        }
        if(fast != null){
    
    
            slow = slow.next;
        }
        while(slow != null){
    
    
            if(!stack.isEmpty() && stack.peek().val == slow.val){
    
    
                stack.pop();
            }
            slow = slow.next;
        }
        return stack.isEmpty();
    }
}
  1. C语言版
bool isPalindrome(struct ListNode* head)
{
    
    
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while(fast != NULL && fast->next != NULL)
    {
    
    
        slow = slow->next;
        fast = fast->next->next;
    }
    struct ListNode** stack = (struct ListNode**)malloc(sizeof(struct ListNode*) * 50000);
    int top = -1;
    while(head != slow)
    {
    
    
        stack[++top] = head;
        head = head->next;
    }
    if(fast != NULL)
    {
    
    
        slow = slow->next;
    }
    while(slow != NULL)
    {
    
    
        if(top != -1 && stack[top]->val == slow->val)
        {
    
    
            top--;
        }
        slow = slow->next;
    }
    free(stack);
    return top == -1;
}
  1. Python语言版
class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        slow = head
        fast = head
        while fast != None and fast.next != None:
            slow = slow.next
            fast = fast.next.next
        stack = []
        while head != slow:
            stack.append(head)
            head = head.next
        if fast != None:
            slow = slow.next
        while slow != None:
            if len(stack) != 0 and stack[-1].val == slow.val:
                stack.pop()
            slow = slow.next
        return len(stack) == 0
  1. C++语言版
class Solution {
    
    
public:
    bool isPalindrome(ListNode* head) {
    
    
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast != nullptr && fast->next != nullptr){
    
    
            slow = slow->next;
            fast = fast->next->next;
        }
        stack<ListNode*> st;
        while(head != slow){
    
    
            st.push(head);
            head = head->next;
        }
        if(fast != nullptr){
    
    
            slow = slow->next;
        }
        while(slow != nullptr){
    
    
            if(!st.empty() && slow->val == st.top()->val){
    
    
                st.pop();
            }
            slow = slow->next;
        }
        return st.empty();
    }
};

十二【提交结果】

  1. Java语言版
    请添加图片描述

  2. C语言版
    在这里插入图片描述

  3. Python语言版
    在这里插入图片描述

  4. C++语言版
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/IronmanJay/article/details/131893759