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
1y

The task is to rearrange the nodes of a singly linked list in a specific order without altering the data of the nodes.

  • Iterate through the linked list to find the middle node using the slow and fast po...read more

Help your peers!
Select
Add answer anonymously...

Springworks Software Developer interview questions & answers

A Software Developer was asked Q. What is object-oriented programming?
A Software Developer was asked Q. What do you know about OOPs?
A Software Developer was asked Q. Tell me about encapsulation.

Popular interview questions of Software Developer

A Software Developer was asked Q1. What is object-oriented programming?
A Software Developer was asked Q2. What do you know about OOPs?
A Software Developer was asked Q3. Tell me about encapsulation.
Springworks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits