Leetcode 143: Reorder List

grid47
grid47
Exploring patterns and algorithms
Oct 23, 2024 7 min read

A series of list items gently swapping positions, forming a balanced sequence.
Solution to LeetCode 143: Reorder List Problem

You are given the head of a singly linked list. The goal is to reorder the list such that the nodes are arranged as follows: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …, without modifying the values of the nodes. Only the structure of the list can be changed.
Problem
Approach
Steps
Complexity
Input: The input consists of a singly linked list with integer values. The linked list is given by its head node.
Example: Input: head = [10, 20, 30, 40]
Constraints:
• The number of nodes in the list is in the range [1, 5 * 10^4].
• 1 <= Node.val <= 1000
Output: The output should be the reordered linked list where nodes are arranged as L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ...
Example: Output: [10, 40, 20, 30]
Constraints:
• The output should be a singly linked list with the nodes rearranged as described.
Goal: The goal is to reorder the linked list as per the given structure without modifying the node values.
Steps:
• 1. Find the middle of the list using the slow and fast pointer approach.
• 2. Reverse the second half of the list.
• 3. Merge the first half and the reversed second half by alternating nodes from each half.
Goal: The solution must operate within the given constraints and reorder the list in linear time.
Steps:
• The list must be reordered in O(n) time and O(1) extra space.
Assumptions:
• The input list is non-empty and contains valid integers.
• The list is not cyclic.
Input: Input: head = [10, 20, 30, 40]
Explanation: In this example, the list is reordered as [10, 40, 20, 30], where the first node is followed by the last node, then the second node, followed by the second last node, and so on.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus