2019-02-28 algorithm leetcode -isPalindromeLinkedList Leetcode题解之 —— 回文链表 思路 O(n)空间复杂度 数组存储链表的值 转化为回文数解法 O(1)空间复杂度 双字符串保存正反向的val 比对该两者 题解 O(n): 12345678910111213141516171819/** * @param {ListNode} head * @return {boolean} */var isPalindrome = function(head) { const cache = []; let current = head; while (current) { cache.push(current.val); current = current.next; } if (cache.length === 1) { return true; } return cache.join('') === cache.reverse().join('');}; O(1): 123456789101112131415/** * @param {ListNode} head * @return {boolean} */var isPalindrome = function(head) { let [forwardStr, reverseStr, current] = ['', '', head]; while (current) { forwardStr += current.val; reverseStr = '' + current.val + reverseStr; current = current.next; } return forwardStr === reverseStr;}; 作者 : zhaoyang Duan 地址 : https://ddzy.github.io/blog/2019/02/28/leetcode-isPalindromeLinkedList/ 来源 : https://ddzy.github.io/blog 著作权归作者所有,转载请联系作者获得授权。