💥[EXPLAINED] TypeScript. Runtime beats 95.90%, Memory beats 27.44%
Intuition\nThe goal is to reorder the linked list so that the nodes are arranged in an alternating pattern from the beginning and end towards the center. This i
# Intuition\nThe goal is to reorder the linked list so that the nodes are arranged in an alternating pattern from the beginning and end towards the center. This involves splitting the list, reversing the second half, and merging the two halves.\n\n# Approach\nFind the Middle: Use a fast and slow pointer to find the middle of the list.\n\nReverse the Second Half: Reverse the second half of the list starting from the middle.\n\nMerge the Halves: Merge the first half with the reversed second half to achieve the reordered list.\n\n# Complexity\n- Time complexity:\nO(n): Traversing the list, reversing the second half, and merging the halves all take linear time.\n\n- Space complexity:\nO(1): The reordering is done in place, using only a constant amount of extra space.\n\n# Code\n```typescript []\nfunction reorderList(head: ListNode | null): void {\n    if (!head || !head.next || !head.next.next) return;\n\n    // Step 1: Find the middle of the linked list\n    let slow = head;\n    let fast = head;\n    while (fast && fast.next) {\n        slow = slow!.next!;\n        fast = fast.next.next;\n    }\n\n    // Step 2: Reverse the second half of the list\n    let second = slow.next;\n    slow.next = null; // Split the list into two halves\n    let prev: ListNode | null = null;\n    while (second) {\n        const next = second.next;\n        second.next = prev;\n        prev = second;\n        second = next;\n    }\n    let reversedSecond = prev;\n\n    // Step 3: Merge the two halves\n    let first = head;\n    while (reversedSecond) {\n        const tmp1 = first.next;\n        const tmp2 = reversedSecond.next;\n\n        first.next = reversedSecond;\n        reversedSecond.next = tmp1;\n\n        first = tmp1;\n        reversedSecond = tmp2;\n    }\n}\n\n```
['TypeScript']