-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalindrome-linked-list_0619.html
46 lines (41 loc) · 1.1 KB
/
palindrome-linked-list_0619.html
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
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>palindrome-linked-list/</title>
</head>
<body></body>
<script>
/**
* 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}
*/
var isPalindrome = function (head) {
if (!head.next) return true;
let nums = [];
while (head) {
nums.push(head.val);
head = head.next;
}
if (nums.length % 2 !== 0) {
nums.splice(nums.length / 2, 1);
}
const nums_cp = [...nums];
let nums1 = nums.splice(0, Math.ceil(nums.length / 2)).join("");
let nums2 = nums_cp
.splice(Math.ceil(nums_cp.length / 2), nums_cp.length)
.reverse()
.join("");
return nums1 == nums2;
};
</script>
</html>