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:

Sample Multi-level 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.

Be the first one to answer
Add answer anonymously...
Goldman Sachs Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter