LeetCode 之 Palindrome Linked List — C++ 实现

原创 2015年07月10日 21:34:17

Palindrome Linked List

 

Given a singly linked list, determine if it is a palindrome.

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

给定一个单链表,判断是不是回文。

追问:可否实现时间复杂度为 O(n),空间复杂度为 O(1)。


分析:

    根据要求,需要在原链表上进行操作。可以将链表前半部分翻转,然后再和后半部分进行比较。

注意:要判断节点个数是偶数还是奇数,奇数个节点时中间节点不参与比较。

class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(!head || !head->next)//空或者一个节点
        {
            return true;
        }
        
        ListNode *pre = NULL, *ne = NULL;//pre指向已经反转好的头节点
        ListNode *phalf = head, *p = head;
        bool ovenNode = false;//判断节点数是偶数还是奇数
        while(p->next) //找出中间节点,并将前一半节点翻转
        {
            ne = phalf;
            phalf = phalf->next;
            p = p->next;
            if(p->next)//p移动两次,phalf移动一次
            {
                p = p->next;
            }
            else
            {
                ovenNode = true;
            }
            
            if(ne == head)//第一次不翻转
            {
                head->next = NULL;
                pre = head;
            }
            else
            {
                ne->next = pre; //翻转
                pre = ne;
            }
            
        }
        
        if(!ovenNode) //奇数个节点,跳出中间节点不比较
        {
            phalf = phalf->next;
        }
        
        while(phalf) //回文检测
        {
            if(pre->val != phalf->val)
            {
                return false;
            }
            phalf = phalf->next;
            pre = pre->next;
        }
        
        return true;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[LeetCode-234] Palindrome Linked List(回文链表、链表中间节点查找)

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) tim...

LeetCode 234: Palindrome Linked List (回文链表)

Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time a...

Leetcode 234. Palindrome Linked List

234. Palindrome Linked List Total Accepted: 69120 Total Submissions: 225579 Difficulty: Easy ...

(LeetCode 234)Palindrome Linked List

Leetcode第234题目是Palindrome Linked List。题目的描述如下: Given a singly linked list, determine if it is a pali...

<LeetCode OJ> 234. Palindrome Linked List

234. Palindrome Linked List My Submissions Question Total Accepted: 33856 Total Submissions: 129...

LeetCode 234. Palindrome Linked List(对称链表)

原题网址:https://leetcode.com/submissions/detail/58253344/ Given a singly linked list, determine if it...

leetcode No234. Palindrome Linked List

QuestionGiven a singly linked list, determine if it is a palindrome. 判断链表是否为回文Algorithm需要解决的问题: 1、...

[leetcode] Palindrome Linked List

题目链接在此 Given a singly linked list, determine if it is a palindrome. Follow up: Could yo...

234.[LeetCode]Palindrome Linked List

一个要求时间复杂度是 O(n),空间复杂度是 O(1) 的判断链表是否是回文的// 时间和空间复杂度都有限的情况下来做运算 // 还是很简单的,遍历一遍链表,使用两个数,正向和逆向的存储我们的链表(1...

[理解leetcode解法]234. Palindrome Linked List 回文链表

[理解leetcode解法]234. Palindrome Linked List 回文链表 Given a singly linked list, determine if it is a pal...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)