Skip to content

Commit 568cec2

Browse files
G5
1 parent d0b9e81 commit 568cec2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

LeetCode/Palindrome linked list.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
bool isPalindrome(ListNode* head) {
14+
ListNode* temp=head;
15+
vector<int> v;
16+
while(temp){
17+
v.push_back(temp->val);
18+
temp=temp->next;
19+
}
20+
int i=0, j=v.size()-1;
21+
while(i<j){
22+
if(v[i]==v[j]){
23+
i++;
24+
j--;
25+
}else return false;
26+
}
27+
return true;
28+
}
29+
};

0 commit comments

Comments
 (0)