- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding next greater node for each node in JavaScript
Problem
We are required to write a JavaScript function that takes in the head of the linked list as the first and the only argument.
This linkedlist contains numerical data. Each node in the list may have a next larger value: for node_i, next_larger(node_i) is the node_j.val such that j > i, node_j.val > node_i.val, and j is the smallest possible choice. If such a j does not exist, the next larger value is 0.
Our function should prepare and return an array in which the corresponding element is the next greater element for the element in the list.
For example, if the list is −
Then the output should be −
const output = [7, 0, 5, 5, 0];
Output Explanation:
Because the next greater element of 2 is 7, for 7 there is no greater element and so on.
Example
The code for this will be −
class Node{ constructor(data){ this.data = data; this.next = null; }; }; class LinkedList{ constructor(){ this.head = null; this.size = 0; }; }; LinkedList.prototype.add = function(data){ const newNode = new Node(data); let curr if(this.head === null){ this.head = newNode; }else{ curr = this.head; while (curr.next) { curr = curr.next; } curr.next = newNode; }; this.size++; }; const list = new LinkedList(); list.add(2); list.add(7); list.add(4); list.add(3); list.add(5); const nextGreater = (head) => { const arr = []; const res = []; let curr = head; let currentIndex = 0 while(curr){ while (arr.length > 0 && curr.data > arr[arr.length - 1][1]) { const [index] = arr.pop(); res[index] = curr.data; }; arr.push([currentIndex, curr.data]); currentIndex += 1; curr = curr.next; }; for(let i = 0; i < currentIndex; i++){ if(res[i] === undefined){ res[i] = 0; }; }; return res; }; console.log(nextGreater(list.head));
Output
And the output in the console will be −
[ 7, 0, 5, 5, 0 ]
- Related Articles
- Populating Next Right Pointers in Each Node in C++
- Populating Next Right Pointers in Each Node II in C++
- Finding distance to next greater element in JavaScript
- Finding middlemost node of a linked list in JavaScript
- Node in Javascript
- I want to return the next node after this node in a JTree with Java
- Replace a child node with a new node in JavaScript?
- First and last child node of a specific node in JavaScript?
- Child node count in JavaScript?
- Find next right node of a given key in C++
- Add all greater values to every node in a given BST?
- Add all greater values to every node in the given BST
- Removing a node in a Javascript Tree
- Create new linked list from two given linked list with greater element at each node in C++ Program
- Finding next n leap years in JavaScript
