Delete the Middle Node of a Linked List LeetCode solution in JavaScript
1/**2* Definition for singly-linked list.3* function ListNode(val, next) {4* this.val = (val===undefined ? 0 : val)5* this.next = (next===undefined ? null : next)6* }7*/8/**9* @param {ListNode} head10* @return {ListNode}11*/12// Two Pointer: Fast & Slow | O(N) | O(1)13var deleteMiddle = function (head) {14if (!head.next) return null;1516let prev = head;17let slow = head;18let fast = head.next;19let size = 2;2021while (fast && fast.next) {22prev = slow;23slow = slow.next;24fast = fast.next.next;25size += fast ? 2 : 1;26}2728if (size % 2 === 0) slow.next = slow.next.next;29else prev.next = slow.next;3031return head;32};