Palindrome Linked List - JS

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?

Subscribe to see which companies asked this question

Tag

LinkedList, Two Pointers

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time Complexity: O(n)

Space Complexity: O(n)

Loop the array and push them to an array, use two pointers to check the palindrom

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function (head) {
    var arr = [];
    while (head) {
        arr.push(head.val);
        head = head.next;
    }

    var len = arr.length;
    if (len <= 1) {
        return true;
    } else {
        var start = 0, end = len - 1;
        while (start < end) {
            if (arr[start] !== arr[end]) {
                return false;
            } else {
                start++;
                end--;
            }
        }

        return true;
    }
};

Time Complexity: O(n)

Space Complexity: O(1)

- Use fast & slow pointer to find the middle of the LinkedList

- Reverse the second half of the LinkedList

- Compare two lists by two pointers

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function (head) {
    if (head === null || head.next === null) {
        return true;
    }

    // Find the mid of the linked list
    var slow = head;
    var fast = head;
    while (fast && fast.next) {
        slow = slow.next;
        fast = fast.next.next;
    }

    // reverse the second half of the list
    var node = slow;
    var prev = null;
    while (node.next) {
        var next = node.next;
        node.next = prev;

        prev = node;
        node = next;
    }
    node.next = prev;

    // compare head and node
    var runner1 = head, runner2 = node;
    while (runner1 && runner2) {
        if (runner1.val !== runner2.val) {
            return false;
        } else {
            runner1 = runner1.next;
            runner2 = runner2.next;
        }
    }

    return true;
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章