#2487
Remove Nodes From Linked List
pupil · 495 · lc medium +27 · verified · 74.8% accepted · 2,402 likes · top 85%
Description
Given the head of a singly linked list, delete every node that has at least one node with a strictly greater value anywhere to its right. Return the head of the resulting list.
Example 1:
Input: head = [5,2,13,3,8]
Output: [13,8]
Explanation: The nodes that should be removed are 5, 2 and 3.
- Node 13 is to the right of node 5.
- Node 13 is to the right of node 2.
- Node 8 is to the right of node 3.
Example 2:
Input: head = [1,1,1,1]
Output: [1,1,1,1]
Explanation: Every node has value 1, so no nodes are removed.
Code
1
2
3
4
5
6
7
8