Skip to content
LEETFLIX

Find the Minimum and Maximum Number of Nodes Between Critical Points

Solve on LeetCode

Medium · linked-list walk solution for Find the Minimum and Maximum Number of Nodes Between Critical Points. Python, Go, and a step-by-step animation.

01

Problem

Read the prompt, flip through the examples, then steal the idea.

LeetCode 2058. Find the Minimum and Maximum Number of Nodes Between Critical Points. The copyrighted prompt is on LeetCode. This page is a working linked-list walk solution for this problem: editable input, Python, Go, and a step-by-step animation.

Constraints

  • —Player inputs are capped so the animation stays readable.
  • —Match LeetCode's own constraints when you submit there.

Examples

Input

head =
[1, 2, 3, 4]

Output

[4,3,2,1]

Each node is peeled off cur and pushed onto prev.

02

Solution

Python and Go. The walkthrough below runs this same code.

Approach 1Optimal▶ Animated

Find the Minimum and Maximum Number of Nodes Between Critical Points — linked-list walk

Three pointers: prev, cur, next. Flip one edge per step. Built for LeetCode 2058. Find the Minimum and Maximum Number of Nodes Between Critical Points.

You only ever need the next node saved before you rewire cur.next = prev. Walk until cur is nil; prev is the new head.

Algorithm

  1. prev = null, cur = head.
  2. Save next, flip the edge, slide prev and cur forward.
  3. Return prev.
Time
O(n)
Space
O(1)
Solution.py
class Solution:    def findTheMinimumAndMaximumNumberOfNodesBetweenCriticalPoints(self, head: list[int]) -> list[int]:        prev: list[int] = []        cur = head        while cur:            nxt = cur[1:]            prev = [cur[0], *prev]            cur = nxt        return prev
Watch this run

03

Interactive walkthrough

Playing “Find the Minimum and Maximum Number of Nodes Between Critical Points — linked-list walk”. Edit the input, hit Run, then scrub.

Approach: Find the Minimum and Maximum Number of Nodes Between Critical Points — linked-list walk

Loading animation…