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;
};