-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.js
More file actions
35 lines (30 loc) · 890 Bytes
/
palindrome.js
File metadata and controls
35 lines (30 loc) · 890 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
32
33
34
35
/**
* Check if a LinkedList is a Palindrome
*
* Singly-linked lists are already defined with this interface:
* function ListNode(x) {
* this.value = x;
* this.next = null;
* }
*
* @param linkedList A linked list.
* @returns A boolean checking if the linked list is a palindrome.
*/
const isLinkedListAPalindrome = (linkedList) => {
let normal = ""
let reversed = ""
while(!!linkedList){
normal = normal + linkedList.value
reversed = linkedList.value + reversed
linkedList = linkedList.next
}
return normal == reversed
}
/**
* Check if a String is a Palindrome
*
* @param string An input string.
* @returns A boolean checking if the string is a palindrom.
*/
const isStringAPalindrome = (string) => [...string].reverse().join('') === string
module.exports = {isLinkedListAPalindrome, isStringAPalindrome}