143. Reorder List

Description

Given a singly linked list L: L0β†’L1→…→Ln-1β†’Ln, reorder it to: L0β†’Lnβ†’L1β†’Ln-1β†’L2β†’Ln-2→…

You may not modify the values in the list's nodes, only nodes itself may be changed.

Constraints

Approach

Examples

Input: 1->2->3->4

Output: 1->4->2->3

Solutions

// Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;
    
    ListNode(int x) {
        val = x;
        next = null;
    }
}

Follow up

Last updated

Was this helpful?