Leetcode NO.234 Palindrome Linked List

来源:互联网 发布:葫芦娃爷爷 知乎 编辑:程序博客网 时间:2024/05/22 00:41

本题题目要求如下:

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


0 0
原创粉丝点击