Reverse Blocks Problem Statement

You are provided with a Singly Linked List of integers and an integer array B of size N. Each element in the array B indicates a block size. Your task is to reverse the nodes within each block of sizes specified by B in the linked list.

Note:
1. If any block size 'B[i]' exceeds the number of remaining nodes in the list, simply reverse the remaining nodes as one block and ignore the rest of the block sizes in 'B'.

2. Block sizes are contiguous,i.e., if block 'B[i]' ends at a node 'cur', then the next block 'B[i+1]' starts from the node just after 'cur'.

Example:

Input:
Linked list: 1->2->3->4->5
Array B: 3 3 5
Output:
3->2->1->5->4
Explanation:

For the first block of size 3, the nodes 1, 2, 3 are reversed. The next block size of 3 is not applicable because only two nodes (4, 5) are left. We reverse these remaining nodes instead and ignore further block sizes.

Input Format:

The first line of input includes elements of the singly linked list separated by a single space, ending with -1.

The second line contains an integer 'N', indicating the size of the block array 'B'.

The third line provides 'N' space-separated integers representing the block sizes in the array 'B'.

Output Format:

Return the altered linked list as a single-space separated list terminated by -1.

Constraints:

  • 0 ≤ L ≤ 5 * 105
  • -109 ≤ data ≤ 109, and data ≠ -1
  • 1 ≤ N ≤ 5 * 105
  • 0 ≤ B[i] ≤ 5 * 105
Note:

You don't need to print the output; it is internally managed. Focus on implementing the functionality.

AnswerBot
4mo

Reverse nodes within blocks of specified sizes in a singly linked list.

  • Iterate through the linked list and reverse nodes within each block size specified in the array.

  • Handle cases where block size exc...read more

Help your peers!
Select
Add answer anonymously...
Go-Jek Android Engineer 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