Palindrome Linked List

来源:互联网 发布:js获取滚动条的位置 编辑:程序博客网 时间:2024/06/16 07:35

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?

转自: http://www.cnblogs.com/grandyang/p/4635425.html

解法一:
这道题让我们判断一个链表是否为回文链表,那么根据回文串的特点,我们需要比较对应位置的值是否相等,那么我们首先需要找到链表的中点,这个可以用快慢指针来实现。我们使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点。我们还需要用栈,每次慢指针走一步,都把值存入栈中,等到达中点时,链表的前半段都存入栈中了,由于栈的后进先出的性质,就可以和后半段链表按照回文对应的顺序比较了。代码如下:

/** * 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, *fast = head;        stack<int> s;        s.push(head->val);        while (fast->next && fast->next->next) {            slow = slow->next;            fast = fast->next->next;            s.push(slow->val);        }        if (!fast->next) s.pop();        while (slow->next) {            slow = slow->next;            int tmp = s.top(); s.pop();            if (tmp != slow->val) return false;        }        return true;    }};

解法二:
这道题的Follow Up让我们用O(1)的空间,那就是说我们不能使用stack了,那么如果代替stack的作用呢,用stack的目的是为了利用其后进先出的特点,好倒着取出前半段的元素。那么现在我们不用stack了,如何倒着取元素呢。我们可以在找到中点后,将后半段的链表翻转一下,这样我们就可以按照回文的顺序比较了,参见代码如下:

class Solution {public:    bool isPalindrome(ListNode* head) {        if (!head || !head->next) return true;        ListNode *slow = head, *fast = head;        while (fast->next && fast->next->next) {            slow = slow->next;            fast = fast->next->next;        }        ListNode *last = slow->next, *pre = head;        while (last->next) {            ListNode *tmp = last->next;            last->next = tmp->next;            tmp->next = slow->next;            slow->next = tmp;        }        while (slow->next) {            slow = slow->next;            if (pre->val != slow->val) return false;            pre = pre->next;        }        return true;    }};