Given a linked list L of integers, the task is to return a linked list of integers such that it contains next greater element for each element in the given linked list. If there doesn’t any greater element for any element then insert 0 for it.
Naive Approach: The naive approach is traverse the linked list L, and for each element in the linked list find the next greater element in the list by traversing the whole string from the current element.
Time Complexity: O(N2)
Efficient Approach: The above naive approach can be optimized by maintaining a monotonically decreasing stack of elements traversed. If a greater element is found append it to the resultant linked list L’ else append 0. Below are the steps:
- Push the first node to stack.
- Pick the rest of the node one by one and follow the following steps in the loop:
- Mark the current node as next node.
- If the stack is not empty, compare the top node value of the stack with next node value.
- If next node value is greater than the top node value then, Pop the top node from the stack and next is the next greater element for the popped node.
- Keep popping the node from the stack while the popped node value is smaller than next node value. next node will becomes the next greater element for all such popped node.
- Finally, push the next node in the stack.
- After the loop in step 2 is over, pop all the node from the stack and print 0 as the next element for them.
Below is the implementation of the above approach:
3 3 5 5 0
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Create new linked list from two given linked list with greater element at each node
- Sum of nodes in a linked list which are greater than next node
- Clone a linked list with next and random pointer | Set 1
- Clone a linked list with next and random pointer | Set 2
- Point to next higher value node in a linked list with an arbitrary pointer
- Clone a linked list with next and random pointer in O(1) space
- Create a linked list from two linked lists by choosing max element at each position
- Replace every element of the array by its next element
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete linked list nodes which have a greater value on left side
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Construct a Doubly linked linked list from 2D Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : iravimandalia