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?
判断单链表是否是回文的
C++(25ms):
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* temp ; 12 bool isPalindrome(ListNode* head) { 13 temp = head ; 14 return check(head) ; 15 } 16 17 bool check(ListNode* p){ 18 if (p == NULL) 19 return true ; 20 bool flag = check(p->next) ; 21 flag = (p->val == temp->val) && flag ; 22 temp = temp->next ; 23 return flag ; 24 } 25 };