Update appNew update is available. Click here to update.

Left Short

Contributed by
Prashansa
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 50 mins
Success Rate 32 %
Share
21 upvotes

Problem Statement

You are given a Singly Linked List of integers. Modify the linked list by removing the nodes from the list that have a greater valued node on their adjacent left in the given linked list.

Detailed explanation ( Input/output format, Notes, Images )
Constraints:
0 <= L <= 5 * 10^5
-10^9 <= node.data <= 10^9 and data != -1

Where 'L' is the number of nodes in the Linked-List.

Time Limit : 1 sec
Sample Input 1:
10 14 6 80 55 56 77 -1
Sample Output 1:
10 14 80 56 77 -1
Explanation For The Sample Output 1:
For the given linked list, nodes with the values 6 and 55 have a greater valued node to their left (14 and 80 respectively). 

Thus, we remove these 2 nodes from the linked list and get the modified list as shown in the output.

Note that the node with the value 56 should not be deleted as its left adjacent node has the value 55 which is not greater than 56.
Sample Input 2:
80 70 60 50 -1
Sample Output 2:
80 -1
Explanation For The Sample Output 2:
For the given linked list, nodes with the values 70, 60, and 50 have a greater valued node to their left (80, 70, and 60 respectively). 

Thus, we remove these 3 nodes from the linked list and get the modified list as shown in the output.
Reset Code
Full screen
Auto
copy-code
Console