Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2058: Find the Minimum and Maximum Number of Nodes Between Critical Points

In this guide, we solve Leetcode #2058 Find the Minimum and Maximum Number of Nodes Between Critical Points in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

A critical point in a linked list is defined as either a local maxima or a local minima. A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Linked List

Intuition

Linked list problems often require pointer manipulation rather than extra memory.

Two-pointer techniques expose cycles, midpoints, or reordering patterns.

Approach

Traverse with fast/slow pointers or reverse sublists when needed.

Maintain invariants carefully to avoid losing nodes.

Steps:

  • Traverse with pointers.
  • Reverse or split if required.
  • Reconnect nodes correctly.

Example

Input: head = [3,1] Output: [-1,-1] Explanation: There are no critical points in [3,1].

Python Solution

# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]: ans = [inf, -inf] first = last = -1 i = 0 while head.next.next: a, b, c = head.val, head.next.val, head.next.next.val if a > b < c or a < b > c: if last == -1: first = last = i else: ans[0] = min(ans[0], i - last) last = i ans[1] = max(ans[1], last - first) i += 1 head = head.next return [-1, -1] if first == last else ans

Complexity

The time complexity is O(n)O(n)O(n), where nnn is the length of the linked list. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy