Rearrange Linked List Problem Statement

Given a singly linked list in the form 'L1' -> 'L2' -> 'L3' -> ... 'Ln', your task is to rearrange the nodes to the form 'L1' -> 'Ln' -> 'L2' -> 'Ln-1', etc. You must not alter the data of the nodes.

Example:

Input:
1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output:
1 -> 5 -> 2 -> 4 -> 3 -> NULL

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case consists of elements of the linked list separated by spaces and terminated by -1.

Output:

For each test case, output a single line of the linked list in rearranged form, separated by spaces and terminated by -1.

Constraints:

  • 1 <= 'T' <= 10
  • 0 <= 'L' <= 1000
  • 1 <= data <= 109 and data != -1

Note:

You do not need to print anything; the function implementation is required.

Follow Up:
Try to solve this problem in O(N) time complexity and O(1) space complexity.
AnswerBot
10d

Rearrange the nodes of a singly linked list in a specific order without altering the data of the nodes.

  • Use two pointers to split the linked list into two halves.

  • Reverse the second half of the linked l...read more

Help your peers!
Add answer anonymously...
Hike Android Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter