
简单给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
示例 1:
输入:head = [1,2,2,1]
输出:true
示例 2:
输入:head = [1,2]
输出:false
作者:力扣 (LeetCode)
链接:https://leetcode-cn.com/leetbook/read/linked-list/fov6t/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/*** 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}*/var isPalindrome = function (head) {if (!head.next) {return true;}const a = [];let p = head;while (p !== null) {a.push(p.val);p = p.next;}let left = 0;let right = a.length - 1;while (left < right) {if (a[left] !== a[right]) {return false;}left++;right--;}return true;};
