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.
Be the first one to answer
Add answer anonymously...
Top Go-Jek Android Engineer interview questions & answers
Popular interview questions of Android Engineer
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app