Reorder List

You are given a singly linked list L: L0→L1→…→Ln-1→Ln. Reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…. You must do this in-place without altering node values. Example: Input: [1,2,3,4] Output: [1,4,2,3] Explanation: After ordering, 1→4→2→3. Pattern focus: Pointer Rewiring (split and merge).

Input Format

head = ListNode

Output Format

return ListNode

Constraints

  • The number of nodes is in the range [1, 10^4].

Examples

Example 1:

Input:

head = [1,2,3,4]

Output:

[1,4,2,3]

Explanation:

Reordered by weaving end nodes into front.

Loading...
Reorder List - Linked List DSA Problem