【leetcode】回文链表

 

编写一个函数,检查输入的链表是否是回文的。

示例 1:

输入: 1->2
输出: false
示例 2:

输入: 1->2->2->1
输出: true

bool isPalindrome(struct ListNode* head){
    if (!head) return true;
    int arr[100000] = {0};
    int pst=0;
    while(head)
    {
        arr[pst++] = head->val;
        head = head->next;
    }
    for (int i=0; i<=pst/2; i++)
    {
        if (arr[i] != arr[pst-1-i]) return false;
    }
    return true;
}

 


 

posted @ 2020-09-13 16:41  温暖了寂寞  阅读(104)  评论(0编辑  收藏  举报