234. Palindrome Linked List
Posted lvbbg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了234. Palindrome Linked List相关的知识,希望对你有一定的参考价值。
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 };
以上是关于234. Palindrome Linked List的主要内容,如果未能解决你的问题,请参考以下文章