-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindromeLinkedList.js
More file actions
31 lines (30 loc) · 841 Bytes
/
palindromeLinkedList.js
File metadata and controls
31 lines (30 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
////////////////////////////////////////////////Palindrome Linked List/////////////////////////////////////////////
// Given the head of a singly linked list, return true if it is a palindrome.
// Example 1:
// Input: head = [1,2,2,1]
// Output: true
// Example 2:
// Input: head = [1,2]
// Output: false
/**
* 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}
*/
const isPalindrome3 = function(head) {
let str = '', rev = '';
while (head) {
str += head.val;
rev = head.val + rev;
head = head.next;
};
return str === rev ? true : false;
};
// console.log(isPalindrome3([1,2,2,1]));
// console.log(isPalindrome3([1,2]));