Skip to content

Latest commit

 

History

History
31 lines (27 loc) · 657 Bytes

File metadata and controls

31 lines (27 loc) · 657 Bytes

Linked List

Screen Shot 2023-03-06 at 12 35 05 PM

Two pointers T.C O(n) S.C O(1)

/**
 * 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 {ListNode}
 */
var middleNode = function(head) {
    let slow = head;
    let fast = head;
    while(fast && fast.next) {
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow
};