Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2 Output: false
Example 2:
Input: 1->2->2->1 Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
这道题是要判断链表是不是回文链表。
1. 我的做法是先判断链表中间在哪,然后把后半段reverse,在比较前半段和reverse之后的后半段。
/**
* 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) {
int length = 0;
for (ListNode * i=head; i!=NULL; i = i -> next )
length ++;
ListNode * middle = head;
if (length % 2 == 0){
for (int i =0; i < length/2; i++)
middle = middle -> next;
}
else {
for (int i =0; i < length/2 + 1; i++)
middle = middle -> next;
}
ListNode * tail = reverse (middle);
for (int i = 0; i < length/2 ; i++){
if(head -> val != tail -> val) return false;
head = head -> next;
tail = tail -> next;
}
return true;
}
ListNode * reverse (ListNode * head ){
if (!head || !head -> next) return head;
ListNode * node = reverse(head->next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
};
2. 别人的做法用了更加巧妙的递归,基本理解了。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode * temp;
bool isPalindrome(ListNode* head) {
temp = head;
return check(head);
}
bool check(ListNode *p){
if (p == NULL) return true;
bool ispal = check(p->next) && (temp->val == p->val);
temp = temp -> next;
return ispal;
}
};
有趣的是,两者所用的时间是一样的。
需要注意一下这道题中提到的空间复杂度的问题。需要慢慢研究品味。