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
4mo

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!
Select
Add answer anonymously...

Hike Android Developer interview questions & answers

An Android Developer was asked 12mo agoQ. What are the different Android components and concepts such as RecyclerView, Vie...read more
An Android Developer was asked 12mo agoQ. Design and implement a Least Recently Used (LRU) cache. It should support the fo...read more
An Android Developer was asked 12mo agoQ. How would you debug and resolve a crash and ANR given a stack trace?

Popular interview questions of Android Developer

An Android Developer was asked 12mo agoQ1. What are the different Android components and concepts such as RecyclerView, Vie...read more
An Android Developer was asked 12mo agoQ2. Design and implement a Least Recently Used (LRU) cache. It should support the fo...read more
An Android Developer was asked 12mo agoQ3. How would you debug and resolve a crash and ANR given a stack trace?
Hike Android 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