Open ZhongKuo0228 opened 1 year ago
https://leetcode.com/problems/palindrome-linked-list/
先轉陣列再使用two pointer來解題
var isPalindrome = function(head) { //先將linked list的值先做成一個array let newArr = []; let curr = head; do{ newArr.push(curr.val); curr = curr.next; }while(curr !== null ) //使用two pointer處理陣列的回文 let left = 0; let right = newArr.length - 1; while(left < right){ if(newArr[left] !== newArr[right]){ return false; } left++; right--; } return true; };
有一個bigO(1)的解法
https://leetcode.com/problems/palindrome-linked-list/
先轉陣列再使用two pointer來解題