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() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- bool isPalindrome(ListNode* head) {
- ListNode* A=head;
- if(!A||!A->next) return true;
- ListNode* slow=A;
- ListNode* fast=A;
- ListNode* back=NULL;
- ListNode* front=A->next;
- while(fast->next!=NULL&&fast->next->next!=NULL)
- {
- fast=fast->next->next;
- back=slow;
- slow=front;
- front=front->next;
- slow->next=back;
- }
- ListNode* left=NULL;
- ListNode* right=NULL;
- if(fast->next==NULL)
- {
- left=slow->next;
- right=front;
- }
- else
- {
- left=slow;
- right=front;
- }
- while(left!=NULL&&right!=NULL)
- {
- if(left->val!=right->val) return false;
- left=left->next;
- right=right->next;
- }
- return true;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement