LeetCode: Palindrome Linked List Solution
Yeah I skipped the follow-upApproach
Convert to array and check
Implementation
1/**2 * Definition for singly-linked list.3 * function ListNode(val, next) {4 * this.val = (val===undefined ? 0 : val)5 * this.next = (next===undefined ? null : next)6 * }7 */8/**9 * @param {ListNode} head10 * @return {boolean}11 */12var isPalindrome = function (head) {13 let arr = []14 while (head) {15 arr.push(head.val)16 head = head.next17 }18 const N = arr.length19 for (let i = 0; i < Math.floor(N / 2); i++) {20 if (arr[i] !== arr[N - i - 1]) {21 return false22 }23 }2425 return true26}
References
Comments
Loading comments...
Tags
leetcode
linked list
two pointers
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Count Nice Pairs In An Array
Apr 3, 2021
Previous Post
LeetCode: Longest Valid Parentheses
Apr 3, 2021