Next Greater Node in Linked List Problem Statement

In a set of linked ninja villages, the goal is to determine if a stronger ninja exists in the nearest village linked ahead. Given a linked list of 'N' integers, each integer represents the strength of the strongest ninja in a village, starting from the head of the linked list.

For each node, you need to find the next larger value, i.e., a village with a stronger ninja.

Example:

Input:
2 -> 1 -> 5
Output:
ans = [5, 5, 0]
Explanation:

For the first node village, the village with a stronger ninja is 5, so ans[0] = 5. Similarly, ans[1] = 5, and since no village has a stronger ninja after node 3, ans[2] = 0.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • 1 <= node.val <= 10^9
  • Time Limit: 1 sec

Input:

The first line contains a single integer 'T' representing the number of test cases. The second line contains space-separated integers for the linked list elements, ending with -1 to denote the end (or null element).

Output:

Return an array of integers answer, where ans[i] = next_larger(node_{i+1}). Output each test case in a separate line.

Note:

You do not need to print anything; the function is implemented to handle the output.
Be the first one to answer
Add answer anonymously...
MasterCard Full Stack Engineer 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