LeetCode: Palindrome Linked List Solution

Yeah I skipped the follow-up

Approach

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} head
10 * @return {boolean}
11 */
12var isPalindrome = function (head) {
13 let arr = []
14 while (head) {
15 arr.push(head.val)
16 head = head.next
17 }
18 const N = arr.length
19 for (let i = 0; i < Math.floor(N / 2); i++) {
20 if (arr[i] !== arr[N - i - 1]) {
21 return false
22 }
23 }
24
25 return true
26}

References

Original problem

Comments

Loading comments...

Tags

leetcode

linked list

two pointers

Next Post

LeetCode: Count Nice Pairs In An Array

HoningJS

Search Posts