{"record":{"id":"1686a0cb3ba29bcc","repo":"TheAlgorithms/Go","slug":"right-boundary-cannot-be-greater-than-the-length-o","errorCode":null,"errorMessage":"right boundary cannot be greater than the length of the linked list","messagePattern":"right boundary cannot be greater than the length of the linked list","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/linkedlist/singlylinkedlist.go","lineNumber":169,"sourceCode":"\t\tpre = pre.Next\n\t}\n\tcur := pre.Next\n\tfor i := 0; i < right-left; i++ {\n\t\tnext := cur.Next\n\t\tcur.Next = next.Next\n\t\tnext.Next = pre.Next\n\t\tpre.Next = next\n\t}\n\tll.Head = tmpNode.Next\n\treturn nil\n}\nfunc (ll *Singly[T]) CheckRangeFromIndex(left, right int) error {\n\tif left > right {\n\t\treturn errors.New(\"left boundary must smaller than right\")\n\t} else if left < 1 {\n\t\treturn errors.New(\"left boundary starts from the first node\")\n\t} else if right > ll.length {\n\t\treturn errors.New(\"right boundary cannot be greater than the length of the linked list\")\n\t}\n\treturn nil\n}\n\n// Display prints out the elements of the list.\nfunc (ll *Singly[T]) Display() {\n\tfor cur := ll.Head; cur != nil; cur = cur.Next {\n\t\tfmt.Print(cur.Val, \" \")\n\t}\n\n\tfmt.Print(\"\\n\")\n}\n","sourceCodeStart":151,"sourceCodeEnd":182,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/linkedlist/singlylinkedlist.go#L151-L182","documentation":"Singly.CheckRangeFromIndex ensures the right bound does not exceed the list's current length, since ReversePartition walks exactly right-left+1 nodes; overshooting would dereference a nil Next pointer and panic.","triggerScenarios":"Calling ReversePartition(left, right) with right > ll.length — e.g. using a stale length saved before removals, an index from another collection, or user input beyond the list size.","commonSituations":"Caching the list length across mutations; passing slice/array lengths from different data; hard-coded positions that exceed a shorter runtime list.","solutions":["Clamp right to ll.Length() before calling","Re-read the current length after any mutations instead of using a cached value","Validate user-provided positions against the live list length"],"exampleFix":"// before\nll.ReversePartition(left, cachedLen) // list has shrunk\n// after\nright := ll.Length()\nif right > ll.Length() { right = ll.Length() }\nll.ReversePartition(left, right)","handlingStrategy":"validation","validationCode":"if right > ll.Length() {\n    return fmt.Errorf(\"right %d exceeds list length %d\", right, ll.Length())\n}\nll.ReversePartition(left, right)","typeGuard":null,"tryCatchPattern":"if err := ll.CheckRangeFromIndex(left, right); err != nil {\n    return fmt.Errorf(\"invalid range: %w\", err)\n}","preventionTips":["Clamp right to the live ll.Length() right before the call","Avoid caching list lengths across mutations","Validate any user-supplied position against the current length"],"tags":["go","data-structures","linked-list","bounds-check","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}