Leetcode NO.234 Palindrome Linked List

原创 2015年07月11日 04:51:44

本题题目要求如下:

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?

本题的思路其实并不难,但是我一直觉得直接手写一个iterative版本的reverse linked list还是有一点难度的,挺难一次写通过的。。感觉这题应该算是easy里面的难题或者是medium里面简单或者中等的水平。。


算法很简单:

1,找到linkedlist的中点,

2,翻转左边的linkedlist或者右边的linkedlist均可,我是选择翻转右边的

3,然后比较


代码如下:

/**
 * 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 == nullptr or head->next == nullptr) {
            return true;
        }
        ListNode* tmp = head;
        int cnt = 0;
        while (tmp != nullptr) {
            tmp = tmp->next;
            ++cnt;
        }
        ListNode* prev;
        tmp = head;
        for (int i = 0; i < cnt / 2; ++i) {
            prev = tmp;
            tmp = tmp->next;
        }
        prev->next = nullptr;
        
        // reverse the linkedlist
        if (tmp->next != nullptr) {
            ListNode* cur = new ListNode(0);
            ListNode* after = tmp->next;
            cur->next = after;
            tmp->next = nullptr;
            while (after != nullptr) {
                after = after->next;
                cur->next->next = tmp;
                tmp = cur->next;
                cur->next = after;
            }
        }
        
        // compare these two linked list
        while (head != nullptr) {
            if (tmp->val != head->val) {
                return false;
            }
            tmp = tmp->next;
            head = head->next;
        }
        return true;
    }
};

翻转链表的题目在我的博客里面也有详细的讲解和代码,可以直接查。。NO.206


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

相关文章推荐

leetcode No234. Palindrome Linked List

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

[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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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