Search⌘ K
AI Features

Reorder List

Explore how to reorder a singly linked list by rearranging node connections in-place without altering node values. Learn to manipulate linked list pointers efficiently and implement this strategy through interactive exercises, improving your skills in linked list pattern problems.

Statement

Given the head of a singly linked list, reorder the list as if it were folded on itself. For example, if the list is represented as follows:

L0L_{0}L1L_{1}L2L_{2} → … → Ln2L_{n-2}Ln1L_{n-1}LnL_{n}

This is how you’ll reorder it:

L0L_{0}LnL_{n}L1L_{1}Ln1L_{n - 1}L2L_{2}Ln2L_{n - 2} → …

You don’t need to modify the values in the list’s nodes; only the links between nodes need to be changed.

Constraints:

  • The range of number of nodes in the list is [1,500][1, 500].

  • 500-500 \leq Node.value 500\leq 500

Examples

canvasAnimation-image
1 / 4

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Reorder List

1.

What is the output if the following linked list is given as input?

4 → 2 → 7 → 8 → 9 → 0 → 2

A.

2 → 4 → 2 → 0 → 7 → 9 → 8

B.

4 → 2 → 7 → 2 → 8 → 0 → 9

C.

9 → 0 → 2 → 8 → 4 → 2 → 7

D.

4 → 2 → 2 → 0 → 7 → 9 → 8


1 / 4

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > main.cpp
// Definition for a Linked List node
// class ListNode {
// public:
// int val;
// ListNode* next;
// // Constructor
// ListNode(int val = 0, ListNode* next = nullptr);
// };
ListNode* ReorderList(ListNode* head) {
// Replace this placeholder return statement with your code
return head;
}
Reorder List