TheAlgorithms/Go · error
left boundary starts from the first node
Error message
left boundary starts from the first node
What it means
Singly.CheckRangeFromIndex enforces that positions are 1-based node positions, matching ReversePartition's convention that the first node is position 1. A left of 0 or negative would corrupt the traversal (e.g. seeking to a node before the head), so it is rejected with this error.
Source
Thrown at structure/linkedlist/singlylinkedlist.go:167
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
- Convert 0-based indexes to 1-based by adding 1 before the call
- Clamp: if left < 1 { left = 1 }
- Validate user input so positions are at least 1
Example fix
// before ll.ReversePartition(i, j) // i, j are 0-based // after ll.ReversePartition(i+1, j+1) // convert to 1-based node positions
Defensive patterns
Strategy: validation
Validate before calling
if left < 1 {
return errors.New("positions are 1-based; left must be >= 1")
}
ll.ReversePartition(left, right) Try / catch
if err := ll.CheckRangeFromIndex(left, right); err != nil {
return fmt.Errorf("invalid range: %w", err)
} Prevention
- Remember this API is 1-based; convert 0-based indexes (+1)
- Clamp computed positions to a minimum of 1
- Document the 1-based convention where positions originate
When it happens
Trigger: Calling ReversePartition(left, right) with left < 1 — typically passing a 0-based index into this 1-based API, or a computed offset that went negative.
Common situations: Mixing 0-based array indexing habits with this 1-based list API; subtracting from a position without a floor check; deserialized positions starting at 0.
Related errors
- left boundary must smaller than right
- right boundary cannot be greater than the length of the link
- index out of range
- index out of range
- index out of bounds
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/1b8c0086804779f8.
Report an issue: GitHub.