File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments