FunctionRacerProblemsRace

Odd Even Linked List

MediumLinked listsPython, JavaScript, TypeScript, Java, C++

Group the nodes by position, not by value: put all nodes in odd positions (1st, 3rd, 5th, ...) first, followed by all nodes in even positions (2nd, 4th, ...). Inside each group keep the original order. Return the head. The list is passed in as head.

Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.

Example 1

head [1,2,3,4,5]
returns [1,3,5,2,4]

Odd positions hold 1, 3, 5; even positions hold 2, 4.

Example 2

head [2,1,3,5,6,4,7]
returns [2,3,6,7,1,5,4]

Positions, not values, decide the groups.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Linked lists problems