标签:term rom als isp bool fast ast tmp nod
234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
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 bool isPalindrome(ListNode* head) { 12 if (!head || !head->next) return true; 13 ListNode *slow = head, *fast = head; 14 while (fast->next && fast->next->next) { 15 slow = slow->next; 16 fast = fast->next->next; 17 } 18 ListNode *last = slow->next, *pre = head; 19 while (last->next) { 20 ListNode *tmp = last->next; 21 last->next = tmp->next; 22 tmp->next = slow->next; 23 slow->next = tmp; 24 } 25 while (slow->next) { 26 slow = slow->next; 27 if (pre->val != slow->val) return false; 28 pre = pre->next; 29 } 30 return true; 31 } 32 };
标签:term rom als isp bool fast ast tmp nod
原文地址:http://www.cnblogs.com/xumh/p/7750640.html