回文链表
设计一种方式检查一个链表是否为回文链表。
1->2->1
就是一个回文链表。
O(n)的时间和O(1)的额外空间。
class Solution {
public:
/**
* @param head a ListNode
* @return a boolean
*/
ListNode *reverse(ListNode *head) {
// write your code here
if(head == NULL)
return NULL;
ListNode *Next = head->next;
head->next = NULL;
while(Next != NULL)
{
ListNode *tmp = Next->next;
Next->next = head;
head = Next;
Next = tmp;
}
return head;
}
bool isPalindrome(ListNode* head) {
// Write your code here
if(head == NULL)
return true;
//快慢指针寻找链表的中点
ListNode *slow = head, *fast = head;
while(fast->next && fast->next->next)
{
slow = slow->next;
fast = fast->next->next;
}
//翻转后半部分
slow->next = reverse(slow->next);
ListNode *pre = head;
//比较前半部分和后半部分
while(slow->next)
{
slow = slow->next;
if(pre->val != slow->val)
return false;
pre = pre->next;
}
return true;
}
};