Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Odd nodes before even nodes

    Odd nodes before even nodes

    Mediumlinked list

    Group the nodes at odd positions (1st, 3rd, …) first, followed by the nodes at even positions, keeping the order within each group. This is about a node's position, not its value. Use O(1) extra memory.

    Example 1

    Input:  head = [1,2,3,4,5]
    Output: [1,3,5,2,4]

    Example 2

    Input:  head = [2,1,3,5,6,4,7]
    Output: [2,3,6,7,1,5,4]

    Constraints

    • 0 ≤ number of nodes ≤ 10⁴
    • -10⁶ ≤ Node.val ≤ 10⁶

    Submit checks your solution against 9 tests, including hidden ones.

    // Provided by the judge: keep these definitions
    class ListNode {
      constructor(val = 0, next = null) {
        this.val = val
        this.next = next
      }
    }
    
    /**
     * @param {ListNode|null} head
     * @return {ListNode|null}
     */
    function oddEvenList(head) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.