143. Reorder List

Reorder List (LeetCode 143) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Linked List Manipulation Patterns pattern, in the Reordering / Partitioning subpattern.

How to solve Reorder List

Reorder List is a Linked List Reordering and Partitioning problem, part of Linked List Manipulation Patterns. Learn to reorder and partition linked lists. Master techniques like swap pairs, rotate list, and odd-even separation.

Work towards the solution in these steps:

  1. Consider how you might divide the linked list into two parts to facilitate reordering; think about finding a midpoint in the list.
  2. Explore efficient ways to find the middle node of a singly linked list, such as using two pointers moving at different speeds.
  3. Once you have two halves, think about reversing the second half of the list to prepare it for merging.

The reference solution runs in O(n) time and O(n) space.

The Reorder List solution and editorial walks through the full approach with reference code.

Problem statement

You are given the head of a singly linked-list. The list can be represented as:

L0 L1 Ln - 1 Ln

Reorder the list to be on the following form:

L0 Ln L1 Ln - 1 L2 Ln - 2

You may not modify the values in the list's nodes. Only nodes themselves may be changed.

Example 1:

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

Example 2:

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

Constraints:

- The number of nodes in the list is in the range [1, 5 104].

- 1 <= Node.val <= 1000

Topics and companies

Topics: Linked List, Two Pointers, Stack, Recursion.

Reported in interviews at Facebook, Amazon, Splunk.

How to practise Reorder List on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.

Open Reorder List in the code editor, or read the Reorder List editorial for a worked solution with its approach and complexity analysis.

Where Reorder List sits in the DSA pattern sheet

Problems related to Reorder List

Other problems that use the same Reordering / Partitioning and Linked List Manipulation Patterns techniques:

Reorder List FAQ

What is the LeetCode number of Reorder List?

Reorder List is LeetCode problem 143, rated Medium.

What pattern is used to solve Reorder List?

Reorder List is a Linked List Reordering and Partitioning problem. It belongs to the Linked List Manipulation Patterns pattern, in the Reordering / Partitioning subpattern.

What is the time complexity of Reorder List?

The reference solution runs in O(n) time and O(n) space.

Which companies ask Reorder List?

Reorder List has been reported in interviews at Facebook, Amazon, Splunk.

Preparing this workspace.