Skip to content

Commit 85b6371

Browse files
committed
add linked list pallindrome
1 parent fd09810 commit 85b6371

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
/**
3+
* Definition for singly-linked list.
4+
* struct ListNode {
5+
* int val;
6+
* ListNode *next;
7+
* ListNode() : val(0), next(nullptr) {}
8+
* ListNode(int x) : val(x), next(nullptr) {}
9+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isPalindrome(ListNode* head) {
15+
vector<int> contents;
16+
while(head != NULL) {
17+
int val = head->val;
18+
contents.push_back(val);
19+
head = head->next;
20+
}
21+
int low = 0, high = contents.size() - 1;
22+
while(low <= high) {
23+
if(contents[low] == contents[high]){
24+
low++;
25+
high--;
26+
}
27+
else {
28+
return false;
29+
}
30+
}
31+
return true;
32+
}
33+
};

0 commit comments

Comments
 (0)