将链表转换为数组后判断是否是回文。
_Bool isPail(struct ListNode* head ) {
// write code here
int* nums = (int*)malloc(sizeof(int)*100001);
int cnt = 0;
while(head){
nums[cnt++] = head->val;
head = head->next;
}
for(int i = 0; i < cnt/2; i++){
if(nums[i] != nums[cnt-1-i]){
return 0;
}
}
return 1;
}

京公网安备 11010502036488号