Reorder List

Reorder List Problem – Medium Level Problem

You are given the head of a singly linked list for Reorder List Problem:

  • For a linked list of length 7, the initial node positions are: 0,1,2,3,4,5,6
  • Reorder the nodes to follow this pattern: 0,6,1,5,2,4,3
  • In general, for a list of length n, the nodes are rearranged as: 0,n−1,1,n−2,2,n−3,…
Reorder Linked List

Constraints:

  • 1 <= Length of the list <= 1000.
  • 1 <= Node.val <= 1000

Hints to Solve Reorder List

Recommendation for Time and Space Complexity (For Reorder List)

You should aim for a solution with O(n) time and O(1) space, where n is the length of the given list.

Hint 1 :

  • A brute force solution would be to store the node values of the list in an array, reorder the values, and create a new list.
  • Can you think of a better way?
  • Perhaps you can try reordering the nodes directly in place, avoiding the use of extra space.

Hint 2 :

  • For example, consider the list [1, 2, 3, 4, 5]. To reorder the list, we connect the first and last nodes, then continue with the second and second-to-last nodes, and so on.
  • Essentially, the list is split into two halves: the first half remains as is, and the second half is reversed and merged with the first half.
  • For instance, [1, 2] will merge with the reversed [5, 4, 3].
  • Can you figure out a way to implement this reordering process? Maybe dividing the list into two halves could help.

Hint 3 :

  • We can divide the list into two halves using the fast and slow pointer approach, which helps identify the midpoint of the list. This allows us to split the list into two halves, with the heads labeled as l1 and l2.
  • Next, we reverse the second half (l2). After these steps, we proceed to reorder the two lists by iterating through them node by node, updating the next pointers accordingly.

There are mainly 3 approach to solve Reorder List problem:

  1. Brute Force Method
  2. Recursion Method
  3. Reverse And Merge Method

1. Brute Force Method

In this approach, we store all the nodes of the linked list in an array to access them by index. Then, we reorder the nodes by modifying their pointers according to the required pattern. This method is simple but uses extra space for the array.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

2. Recursion Method

Using recursion, we traverse to the end of the list and backtrack to reorder the nodes. By maintaining pointers during backtracking, we adjust the node connections to achieve the desired order. This method avoids additional data structures but requires extra stack space.

  • Time complexity: O(n)
  • Space complexity: O(n)

Code

3. Reverse and Merge Method

Efficient approach splits the list into two halves, reverses the second half, and merges it with the first half in alternating order. It uses the fast and slow pointer technique to divide the list, ensuring O(1) space complexity while achieving the desired reordering.

  • Time complexity: O(n)
  • Space complexity: O(1)

Code

More Articles