Remove Nodes With a Larger Node After
Remove every node that has some node later in the list with a strictly greater value. Return the head of what is left. The last node always stays, and the remaining values never increase from left to right. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [5,2,13,3,8]
returns [13,8]
5 and 2 have 13 after them, 3 has 8 after it. Left: 13, 8.
Example 2
head [1,1,1]
returns [1,1,1]
Equal is not greater.
Constraints
0 <= number of nodes <= 100-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Linked lists problems
- Remove Nth Node From End of ListMedium
- Reorder ListMedium
- Reverse Between PositionsMedium
- Reverse Even Length GroupsMedium
- Reverse the Second HalfMedium
- Rotate ListMedium
- Sort ListMedium
- Split Linked List in PartsMedium