Input: linked list = [2, 1, 5]
Output: [5, 5, -1]
Input: linked list = [2, 7, 4, 3, 5]
Output: [7, -1, 5, 5, -1]
To solve the problem mentioned above the main idea is to use a Stack Data Structure.
- Iterate through the linked list and insert the value and position of elements of linked list into a stack.
- Initialize result vector with -1 for every node.
- Update previous node’s value while current node’s value is greater than previous nodes and pop the value from stack after updating.
Below is the implementation of the above approach:
7, -1, 5, 5, -1,
Time Complexity: O(N)
Auxiliary Space Complexity: 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.
- Point arbit pointer to greatest value right side node in a linked list
- Replace each node with its Surpasser Count in Linked List
- Create new linked list from two given linked list with greater element at each node
- Delete linked list nodes which have a Lesser Value on Left Side
- Delete linked list nodes which have a greater value on left side
- Replace nodes with duplicates in linked list
- Replace even nodes of a doubly linked list with the elements of array
- Swap Kth node from beginning with Kth node from end in a Doubly Linked List
- Create a linked list from two linked lists by choosing max element at each position
- Swap Kth node from beginning with Kth node from end in a Linked List
- Delete Nth node from the end of the given linked list
- Remove every k-th node of the linked list
- Squareroot(n)-th node in a Linked List
- Linked List | Set 3 (Deleting a node)
- Remove last node of the linked list
- Remove first node of the linked list
- Linked List | Set 2 (Inserting a node)
- Program for n'th node from the end of a Linked List
- Remove Nth node from end of the Linked List
- Delete every Kth node from circular linked list
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.