TheAlgorithms/Go · error

left boundary must smaller than right

Error message

left boundary must smaller than right

What it means

Singly.CheckRangeFromIndex validates the [left, right] partition bounds used by ReversePartition. This first check rejects ranges where left exceeds right, since such a range is empty or malformed and reversal would be undefined.

Source

Thrown at structure/linkedlist/singlylinkedlist.go:165

	tmpNode := &Node[T]{}
	tmpNode.Next = ll.Head
	pre := tmpNode
	for i := 0; i < left-1; i++ {
		pre = pre.Next
	}
	cur := pre.Next
	for i := 0; i < right-left; i++ {
		next := cur.Next
		cur.Next = next.Next
		next.Next = pre.Next
		pre.Next = next
	}
	ll.Head = tmpNode.Next
	return nil
}
func (ll *Singly[T]) CheckRangeFromIndex(left, right int) error {
	if left > right {
		return errors.New("left boundary must smaller than right")
	} else if left < 1 {
		return errors.New("left boundary starts from the first node")
	} else if right > ll.length {
		return errors.New("right boundary cannot be greater than the length of the linked list")
	}
	return nil
}

// Display prints out the elements of the list.
func (ll *Singly[T]) Display() {
	for cur := ll.Head; cur != nil; cur = cur.Next {
		fmt.Print(cur.Val, " ")
	}

	fmt.Print("\n")
}

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Normalize bounds before calling: if left > right { left, right = right, left }
  2. Validate the range in the caller and return a clearer domain error
  3. Fix the argument order at the call site

Example fix

// before
ll.ReversePartition(right, left) // swapped
// after
if left > right {
    left, right = right, left
}
ll.ReversePartition(left, right)
Defensive patterns

Strategy: validation

Validate before calling

if left > right {
    left, right = right, left // normalize
}
ll.ReversePartition(left, right)

Try / catch

if err := ll.CheckRangeFromIndex(left, right); err != nil {
    return fmt.Errorf("reverse partition [%d,%d]: %w", left, right, err)
}

Prevention

When it happens

Trigger: Calling ReversePartition(left, right) with left > right — e.g. swapping arguments accidentally, or computing bounds from variables where the smaller/larger order was not normalized.

Common situations: User-supplied range input (start/end) not normalized; sorting two variables incorrectly; off-by-sign errors when indexes are derived from other data.

Related errors


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/2e572171f7a48032. Report an issue: GitHub.