Flatten the Multi-Level Linked List Problem
You are provided with a multi-level linked list consisting of 'N' nodes. Each node contains a 'next' and 'child' pointer that may point to another node. Your task is to flatten this multi-level linked list into a singly linked list and return the head of the modified linked list.
Input:
The first line contains an integer 'T' denoting the number of test cases. Each test case starts with a line containing elements of the multi-level linked list in level order. Node values are separated by spaces. For non-existent nodes (next or child), use -1. For example, for a certain multi-level linked list:
10
5 4
12 -1 20 -1
7 -1 13 2
11 17 -1 16 -1 -1
-1 -1 -1 -1 -1 -1
Output:
For each test case, output the flattened linked list in a single line.
Example:
Consider the input for the following multi-level linked list:

Input: 10 5 4 12 -1 20 -1 7 -1 13 2 11 17 -1 16 -1 -1 -1 -1 -1 -1 -1 -1
Output: 10 5 12 7 11 17 4 20 13 2 16
Constraints:
- 1 <= T <= 10
- 0 <= N <= 104
- 1 <= data <= 109
Note: You are not required to print anything. The implementation of the function should handle this internally.

AnswerBot
4mo
The task is to flatten a multi-level linked list into a singly linked list and return the head of the modified linked list.
Traverse the linked list and keep track of nodes with child pointers
Flatten t...read more
Help your peers!
Add answer anonymously...
Goldman Sachs Software Analyst interview questions & answers
A Software Analyst was asked Q. Describe how to implement two stacks using a single array.
A Software Analyst was asked Q. Design a stack that supports push, pop, top, and retrieving the minimum element ...read more
A Software Analyst was asked Q. What is the difference between SQL and NoSQL databases?
Popular interview questions of Software Analyst
A Software Analyst was asked Q1. Describe how to implement two stacks using a single array.
A Software Analyst was asked Q2. Design a stack that supports push, pop, top, and retrieving the minimum element ...read more
A Software Analyst was asked Q3. What is the difference between SQL and NoSQL databases?
>
Goldman Sachs Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

