Palindrome Linked List leetcode 234

原创 2015年07月21日 09:31:36

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?

判断给定的链表是不是回文的:

把要判断的链表分成两部分,将第二部分逆置,逐个与前一部分的val相比较,若不同返回false

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(!head||!head->next)return true;

        ListNode* slow=head;//这里定义slow,fast指针是为了寻找链表中间的位置
        ListNode* fast=head;
        while(fast->next&&fast->next->next){//假如链表有奇数个元素,则slow指针在循环后指向正中的元素;
        //若链表有偶数个元素,则循环后slow指向n/2的元素位置
            slow=slow->next;
            fast=fast->next->next;
        }
        slow->next=reverse(slow->next);//将链表后面部分逆置
        slow=slow->next;
        while(slow){
            if(head->val!=slow->val)return false;
            head=head->next;
            slow=slow->next;
        }
        return true;
    }
    ListNode* reverse(ListNode* head){
        if(!head||!head->next)return head;
        ListNode* pre=NULL;
        ListNode* next=NULL;
        while(head){
            next=head-next;
            head->next=pre;
            pre=head;
            head=next;
        }
    return pre;
    }
};

贴个递归解法

bool isPalindrome(ListNode* head) {
    if (!head || !head->next) return true;
    bool flag = true;
    recur_pal(head, head->next, flag);
    return flag;
}
void recur_pal(ListNode *&head, ListNode *tail, bool &flag) {
    if (head == tail || head == tail->next) return;
    if (tail->next) {
        recur_pal(head, tail->next, flag);
    }
    if (head->val != tail->val) { flag  = false; return;}
        head = head->next;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

[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】234. Palindrome Linked List

思路: 1、遍历整个链表,将链表每个节点的值记录在数组中,再判断数组是不是一个回文数组,时间复杂度为O(n),但空间复杂度也为O(n),不满足空间复杂度要求。 2、利用栈先进后出的性质,将链表前半段压...

[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)...

Leetcode_234_Palindrome Linked List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334465 Given a singly lin...

LeetCode笔记:234. Palindrome Linked List

判断一个链表是否是回文

leetcode234~Palindrome Linked List

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

[leetcode] 234.Palindrome Linked List

题目: Given a singly linked list, determine if it is a palindrome. 题意: 给定一个链表,查看是否是回文的。 思路: 最简单的思路...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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