-
Notifications
You must be signed in to change notification settings - Fork 1
/
palindrome-linked-list.js
80 lines (72 loc) · 1.65 KB
/
palindrome-linked-list.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Source: https://leetcode.com/problems/palindrome-linked-list/
* Tags: [Linked List,Two Pointers]
* Level: Easy
* Title: Palindrome Linked List
* Auther: @imcoddy
* Content: Given a singly linked list, determine if it is a palindrome.
*
* Follow up:
* Could you do it in O(n) time and O(1) space?
*
*
*
*
*
*
*
*
* Show Similar Problems
*
*
* (E) Palindrome Number
*
* (E) Valid Palindrome
*
* (E) Reverse Linked List
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
/**
* Memo: Loop the linked list and store its value in an array, check from the middle.
* Complex: O(n)
* Runtime: 152ms
* Tests: 21 test cases passed
* Rank: A
* Updated: 2015-08-07
*/
var isPalindrome = function(head) {
var array = [];
while (head) {
array.push(head.val);
head = head.next;
}
var length = array.length;
var right = ~~(length / 2);
var left = length % 2 === 0 ? right - 1 : right;
while (left >= 0) {
if (array[left] !== array[right]) {
break;
}
left--;
right++;
}
return left < 0;
};
var util = require("./util.js");
var should = require('should');
console.time('Runtime');
isPalindrome(util.arrayToLinkList([])).should.equal(true);
isPalindrome(util.arrayToLinkList([1, 2, 3, 2, 1])).should.equal(true);
isPalindrome(util.arrayToLinkList([1, 2, 3, 3, 2, 1])).should.equal(true);
isPalindrome(util.arrayToLinkList([1, 2, 3, 2, 2, 1])).should.equal(false);
console.timeEnd('Runtime');