We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difficulty: 简单
Related Topics: 栈, 递归, 链表, 双指针
给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
head
true
false
示例 1:
输入:head = [1,2,2,1] 输出:true
示例 2:
输入:head = [1,2] 输出:false
提示:
0 <= Node.val <= 9
**进阶:**你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
O(n)
O(1)
Language: JavaScript
/** * 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} */ // 双指针 // 首先将链表转为数组 // 左指针指向第一个节点 // 右指针指向最后一个节点 // 比较两个指针指向的节点中的数据 // 在两个指针相遇之前没有遇到不相等的节点则返回true,否则返回false var isPalindrome = function(head) { let arr = []; while(head){ arr.push(head.val); head = head.next; } let left = 0, right = arr.length-1; while(left <= right){ if(arr[left] != arr[right]) return false; left++; right--; } return true; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
234. 回文链表
Description
Difficulty: 简单
Related Topics: 栈, 递归, 链表, 双指针
给你一个单链表的头节点
head
,请你判断该链表是否为回文链表。如果是,返回true
;否则,返回false
。示例 1:
示例 2:
提示:
0 <= Node.val <= 9
**进阶:**你能否用
O(n)
时间复杂度和O(1)
空间复杂度解决此题?Solution
Language: JavaScript
The text was updated successfully, but these errors were encountered: