-
Notifications
You must be signed in to change notification settings - Fork 0
/
234. Palindrome Linked List.js
58 lines (50 loc) · 1.3 KB
/
234. 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
/**
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
*/
/**
* Algorithm: Fast and slow pointers
* 1. User fast and slow pointers to find the mid node
* 2. Reverse the first half of the list while finding the mid node
* 3. Compare the first half with the second half
*
* T: O(n)
* S: O(1)
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
const isPalindrome = function(head) {
if (head === null) return true;
let reverseHead = null;
let fast = head;
let slow = head;
// 1. and 2. Find mid node and reverse first half
while (fast !== null && fast.next !== null) {
let temp = reverseHead;
reverseHead = slow;
slow = slow.next;
fast = fast.next.next;
reverseHead.next = temp;
}
// Odd number list, let right half be smaller
if (fast !== null) {
slow = slow.next;
}
// 3. Compare first half and second half
while (reverseHead !== null) {
if (reverseHead.val !== slow.val) return false;
slow = slow.next;
reverseHead = reverseHead.next;
}
// Finish comparing the both half of lists and not returning false
return true;
}