Skip to content

Commit 0beebc5

Browse files
Time: 282 ms (14.06%), Space: 114 MB (81.72%) - LeetHub
1 parent ecdf407 commit 0beebc5

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* reverse (ListNode* curr){
14+
ListNode* cn=curr;
15+
ListNode* prev= nullptr;
16+
while(curr != NULL){
17+
cn=curr->next;
18+
curr->next=prev;
19+
prev=curr;
20+
curr=cn;
21+
22+
}
23+
return prev;
24+
25+
}
26+
27+
bool isPalindrome(ListNode* head) {
28+
if(head->next== NULL) return head;
29+
ListNode *fast=head, *slow= head;
30+
while(fast != nullptr and fast ->next != nullptr){
31+
fast=fast->next->next;
32+
slow=slow->next;
33+
}
34+
slow=reverse(slow);
35+
fast=head;
36+
37+
while(slow != nullptr)
38+
{
39+
if(slow->val != fast->val){
40+
return false;
41+
}
42+
slow=slow->next;
43+
fast=fast->next;
44+
}
45+
return true;
46+
}
47+
};

0 commit comments

Comments
 (0)