/**
* 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 ans = false;
bool isPalindrome(ListNode* head) {
ListNode* f = head;
helper(f, head);
...
return somthing....
}
void helper(ListNode* f, ListNode* b) {
if (!b->next)
return f->val == b->val;
int f_test = f->val;
int b_test = b->val;
bool ret = helper(f, b->next);
int f_test1 = f->val;
int b_test1 = b->val;
cout<<f_test <<": "<< f_test1<<endl;
f = f->next;
}
};
This helper function is used to check if a LinkedList is Palindrome. so I want to use two pointers one point to the end and another pointing to the beginning. and check the values. to simplify my issue I removed the logic after f = f->next;
Actually, my question is why the f is not changed since I have f = f->next. so the f_test and f_test1 should be different.
if the input is [1,2,,3, 4] in my understanding, the print list should be:
1: 2
2: 3
3: 4
but actually, it's not like that this.
is someone could help me with this?