15 · Linked Lists

#328 Odd Even Linked List

Mediumsolve on LeetCode ↗

frequently reported atAmazonMicrosoft

the problem

In plain words

Rearrange a singly linked list so that the nodes at odd positions (1st, 3rd, 5th, …) come first and the nodes at even positions follow, each group keeping its original order. Positions matter, not values. Use O(1) extra space and O(n) time.

in head = [1,2,3,4,5]

out [1,3,5,2,4]

in head = [2,1,3,5,6,4,7]

out [2,3,6,7,1,5,4]

Positions 1, 3, 5, 7 hold 2, 3, 6, 7; positions 2, 4, 6 hold 1, 5, 4.

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

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #86
      Partition List
      Mediumin Linked Lists
    2. #143
      Reorder List
      Mediumin Linked Lists
    3. #283
      Move Zeroes
      Easyin Two Pointers