Reorder List

MediumLinked Listin-place reversal

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given the head of a singly linked list L0 -> L1 -> ... -> Ln-1 -> Ln. Rearrange its nodes into the order L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ..., alternating between the front and the back until every node is placed. Rearrange the nodes themselves rather than rewriting values. Unlike the classic version that returns nothing, here you must **return the head** of the reordered list (which is still the original first node) so the grader can read it. Aim for O(n) time and O(1) extra space.

Examples

Input: head = [1,2,3,4]
Output: [1,4,2,3]
Take 1 from the front, 4 from the back, then 2, then 3.
Input: head = [1,2,3,4,5]
Output: [1,5,2,4,3]
With an odd length the middle node 3 ends up last.

Constraints

  • 1 <= number of nodes <= 5 * 10^4
  • 1 <= Node.val <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More in-place reversal problems