Dsa
Linked List
Medium
Peak-Valley Pruning
In a linked list, a node is a 'Peak' if its value is strictly greater than its predecessor and successor. A node is a 'Valley' if its value is strictly less than its predecessor and successor. Traverse the list once and remove all Peaks and Valleys. Note: The head and tail can never be Peaks or Valleys because they lack one neighbor. The removal should happen based on the original neighbors before any deletions in that pass.
Example:
Input: 1 5 2 6 3
- 5 is a peak (1 < 5 > 2)
- 2 is a valley (5 > 2 < 6)
- 6 is a peak (2 < 6 > 3)
Output: 1 3
Key concepts
linked_listdeletionpointers
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now