-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question37.cpp
42 lines (38 loc) · 945 Bytes
/
Question37.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
37 -> Palindrome Linked List
Solution :
class Solution {
public:
ListNode*getmid(ListNode*&head){
ListNode*fast=head->next;
ListNode*slow=head;
while(fast&&fast->next){
fast=fast->next->next;
slow=slow->next;
}
return slow;
}
ListNode*reverse(ListNode*head){
ListNode*curr=head;
ListNode*prev=NULL;
ListNode*forward=NULL;
while(curr){
forward=curr->next;
curr->next=prev;
prev=curr;
curr=forward;
}
return prev;
}
bool isPalindrome(ListNode* head) {
ListNode*mid=getmid(head);
ListNode*head2=mid->next;
mid->next=NULL;
head2=reverse(head2);
while(head2){
if(head2->val!=head->val)return false;
head2=head2->next;
head=head->next;
}
return true;
}
};