判断是否为回文链表

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
const isPalindrome = (head) => {
    if(!head) return true
    const arr = []
    while(head){
        arr.push(head.val)
        head = head.next
    }
    let startIdx = 0, endIdx = arr.length - 1;
    while(startIdx <= endIdx){
        if(arr[startIdx] !== arr[endIdx]){
            return false
        }
        startIdx++
        endIdx--
    }
    return true
};

  

posted @ 2023-02-01 17:38  671_MrSix  阅读(11)  评论(0编辑  收藏  举报