Delete the Middle Node of a Linked List

LeetCode題目: 2095. Delete the Middle Node of a Linked List

My solution:

/**
 * 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}
 */
let deleteMiddle = head => {
    if(head === null || head.next === null) return null;
    let slow = head;
    let fast = head.next;
    while(fast !== null && fast.next !== null && fast.next?.next !== null) {
        slow = slow.next;
        fast = fast.next.next;
    }
    slow.next = slow.next?.next;
    return head;
};
profile-image
Hi, 我是 Zeki。目前為一名前端工程師。我相信科技始終來自於人性,是用來幫助人們過上更有品質的生活的,但願也希望如此。