|
| 1 | +// Palindrome: Implement a function to check if a linked list is a palindrome. |
| 2 | + |
| 3 | +const assert = require("assert"); |
| 4 | +const { arrayToLinkedList } = require("../../lib/avc278/linkedlist"); |
| 5 | + |
| 6 | +/** |
| 7 | + * Checks whether the input linked list is a palindrome |
| 8 | + * @param {LinkedListNode} list input linked list to check against |
| 9 | + * @return {boolean} whether the input linked list is a palindrome |
| 10 | + * |
| 11 | + * For this problem, we start by finding the midpoint idx of the linked list. This traversal takes O(N) runtime and O(1) |
| 12 | + * space as we only store a counter, and a pointer to the location within the linked list. We then perform another |
| 13 | + * traversal to point to the midpoint, and reverse the linked list from that point onwards. From this point onwards, we |
| 14 | + * only need to compare the two linked lists from the midpoint to the end, and from the start to the midpoint. |
| 15 | + * All in all, since we only perform traversals without any inner traversals, the runtime at any traversal is O(N), |
| 16 | + * where N is the length of the linked list, and O(1) space as we store pointers. |
| 17 | + * Runtime: O(N) |
| 18 | + * Space: O(1) |
| 19 | + * |
| 20 | + */ |
| 21 | +const reverse = (node) => { |
| 22 | + let prev; |
| 23 | + while (node !== null) { |
| 24 | + const next = node.next; |
| 25 | + node.next = prev; |
| 26 | + prev = node; |
| 27 | + node = next; |
| 28 | + } |
| 29 | + return prev; |
| 30 | +}; |
| 31 | + |
| 32 | +const isPalindrome = (list) => { |
| 33 | + let fast = list; |
| 34 | + let slow = list; |
| 35 | + let counter = 0; |
| 36 | + while (fast !== null) { |
| 37 | + fast = fast.next; |
| 38 | + counter += 1; |
| 39 | + } |
| 40 | + |
| 41 | + for (let i = 0; i < counter / 2; i++) { |
| 42 | + slow = slow.next; |
| 43 | + } |
| 44 | + |
| 45 | + slow = reverse(slow); |
| 46 | + while (!!slow && !!slow.next) { |
| 47 | + if (slow.val !== list.val) return false; |
| 48 | + slow = slow.next; |
| 49 | + list = list.next; |
| 50 | + } |
| 51 | + |
| 52 | + return true; |
| 53 | +}; |
| 54 | + |
| 55 | +describe(module.filename, () => { |
| 56 | + it("should return true when the linked list of even length is a palindrome", () => { |
| 57 | + const arr = [1, 2, 3, 4, 4, 3, 2, 1]; |
| 58 | + const ll = arrayToLinkedList(arr); |
| 59 | + |
| 60 | + assert.ok(isPalindrome(ll)); |
| 61 | + }); |
| 62 | + it("should return true when the linked list of odd length is a palindrome", () => { |
| 63 | + const arr = [1, 2, 3, 4, 3, 2, 1]; |
| 64 | + const ll = arrayToLinkedList(arr); |
| 65 | + |
| 66 | + assert.ok(isPalindrome(ll)); |
| 67 | + }); |
| 68 | + it("should return false when the linked list is not a palindrome", () => { |
| 69 | + const arr = [1, 2, 3, 4, 3, 2, 1, 0]; |
| 70 | + const ll = arrayToLinkedList(arr); |
| 71 | + |
| 72 | + assert.ok(!isPalindrome(ll)); |
| 73 | + }); |
| 74 | + it("should return true for a linked list with a single node", () => { |
| 75 | + const arr = [2]; |
| 76 | + const ll = arrayToLinkedList(arr); |
| 77 | + |
| 78 | + assert.ok(isPalindrome(ll)); |
| 79 | + }); |
| 80 | + it("should return true for an empty linked list", () => { |
| 81 | + const arr = []; |
| 82 | + const ll = arrayToLinkedList(arr); |
| 83 | + |
| 84 | + assert.ok(isPalindrome(ll)); |
| 85 | + }); |
| 86 | +}); |
0 commit comments