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
- Create new linked list from two given linked list with greater element at each node
- Replace each node with its Surpasser Count in Linked List
- Remove every k-th node of the linked list
- Delete every Kth node from circular linked list
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Delete linked list nodes which have a greater value on left side
- Delete linked list nodes which have a Lesser Value on Left Side
- Delete nodes which have a greater value on right side using recursion
- Delete nodes which have a greater value on right 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 Linked List
- 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
- 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
- Check if a linked list is Circular Linked List
- Convert singly linked list into 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.