-
Notifications
You must be signed in to change notification settings - Fork 0
/
22mar.txt
37 lines (37 loc) · 1.01 KB
/
22mar.txt
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
class Solution {
public:
ListNode *reverse(ListNode * head){
if(!head) return head;
ListNode * prev = NULL;
ListNode *curr = head;
ListNode *nextnode = NULL;
while(curr!=NULL){
nextnode = curr->next;
curr->next = prev;
prev = curr;
curr = nextnode;
}
return prev;
}
bool isPalindrome(ListNode* head) {
if(head == NULL || head->next == NULL) return true;
ListNode* slow = head;
ListNode* fast = head;
while(fast !=NULL && fast->next !=NULL){
fast = fast->next->next;
slow = slow->next;
}
ListNode *ReverseHead = reverse(slow);
ListNode *temp1 = head;
ListNode *temp2 = ReverseHead;
while(temp2!=NULL){
if(temp1->val != temp2->val)
return false;
else{
temp1 = temp1->next;
temp2 = temp2->next;
}
}
return true;
}
};