Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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) {
- if(!head)return true;
- ListNode *slow = head,*fast = head->next;
- while(fast&&fast->next){
- slow = slow->next;
- fast = fast->next->next;
- }
- ListNode *prev = slow;
- ListNode *pp = prev->next;
- prev->next = NULL;
- while(pp){
- ListNode *tmp = pp->next;
- pp->next = prev;
- prev = pp;
- pp = tmp;
- }
- while(head&&prev){
- if(head->val!=prev->val)return false;
- head = head->next;
- prev = prev->next;
- }
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement