Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given a singly linked list, remove all the nodes which have a greater value on its following nodes.

**Example 1:**

**Input:
**LinkedList = 12->15->10->11->5->6->2->3
**Output: **15 11 6 3**
Explanation: **Since, 12, 10, 5 and 2 are
the elements which have greater elements
on the following nodes. So, after deleting
them, the linked list would like be 15,
11, 6, 3.

**Example 2:**

**Input:
**LinkedList = 10->20->30->40->50->60
**Output: **60

**Your Task:**

The task is to complete the function **compute**() which should modify the list as required and return the head of the modified linked list. The **printing **is done by the **driver **code,

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 ≤ size of linked list ≤ 1000

1 ≤ element of linked list ≤ 1000

**Note**: Try to solve the problem without using any extra space.

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Delete nodes having greater value on right

...