Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given a singly linked list*: A*_{0}→*A*_{1}→…→*A*_{n-1}→*A*_{n}, reorder it to: *A*_{0}→*A*_{n}→*A*_{1}→*A*_{n-1}→*A*_{2}→*A*_{n-2}→…

For example: Given 1->2->3->4->5 its reorder is 1->5->2->4->3.

**Note: It is recommended do this in-place without altering the nodes' values.**

**Example 1:**

Input:LinkedList: 1->2->3Output:1 3 2

**Example 2:**

Input:LinkedList: 1->7->3->4Output:1 4 7 3.

**Your Task:**

The task is to complete the function **reorderList**() which should reorder the list as required. The reorder list is automatically printed by the driver's code.

**Note**: Try to solve without using any auxilliary space.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= N <= 10^{4}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Reorder List

...