leetcode - 234. 回文链表
题目
代码
#include <iostream>
#include <vector>
using namespace std;
typedef struct ListNode{
int val;
struct ListNode *next;
}ListNode, *LinkList;
void create(LinkList &head){
int n;
cin>>n;
head = new ListNode;
head->next = NULL;
ListNode *tail = head;
for(int i = 0; i < n; i++){
ListNode *p = new ListNode;
cin>>p->val;
p->next = NULL;
tail->next = p;
tail = p;
}
}
bool isPalindrome(ListNode* head) {
vector<int> nums;
while(head){
nums.push_back(head->val);
head = head->next;
}
int left = 0, right = nums.size() - 1;
while(left <= right){
if(nums[left] != nums[right]){
return false;
}
left++;
right--;
}
return true;
}
int main(){
ListNode *head;
bool res;
create(head);
head = head->next;
res = isPalindrome(head);
if(res){
cout<<"true";
}else{
cout<<"false";
}
return 0;
}