How to Reverse a Sublist in a Linked List (LeetCode 92) Using Recursion in Go

This article explains a recursive solution in Go for LeetCode problem 92, detailing how to reverse a sublist of a singly linked list between given positions by first locating the start node, using a helper to reverse the first n nodes, and correctly reconnecting the remaining parts.

Nullbody Notes
Nullbody Notes
Nullbody Notes
How to Reverse a Sublist in a Linked List (LeetCode 92) Using Recursion in Go

Problem

The task is to reverse the nodes of a singly linked list from position left to position right. The solution must first locate the start of the sub‑list that needs to be reversed.

Recursive Approach

If left == 1, the sub‑list starts at the head, so the problem reduces to reversing the first right nodes. This is delegated to a helper function reverseN. Otherwise, the function calls itself on head.Next with left-1 and right-1, then attaches the returned sub‑list back to the current node.

func reverseBetween(head *ListNode, left int, right int) *ListNode {
    // If the current node is not within the reversal range, recurse on the next node.
    head.Next = reverseBetween(head.Next, left-1, right-1)
    return head
}

Helper Function reverseN

The helper reverses the first n nodes starting from head and returns the new head of this reversed segment. A package‑level variable successor stores the node that follows the reversed part so it can be re‑attached after reversal.

var successor *ListNode = nil

func reverseN(head *ListNode, n int) *ListNode {
    if n == 1 {
        // Record the node after the first reversed node.
        successor = head.Next
        return head
    }
    // Recursively reverse the remaining n‑1 nodes.
    last := reverseN(head.Next, n-1)
    // Rewire pointers to achieve reversal.
    head.Next.Next = head
    head.Next = successor
    return last
}

Putting It All Together

The complete solution first checks whether the reversal starts at the head. If so, it calls reverseN(head, right). Otherwise, it recursively processes the next node and connects the partially reversed sub‑list back to the current node.

func reverseBetween(head *ListNode, left int, right int) *ListNode {
    if left == 1 {
        return reverseN(head, right)
    }
    head.Next = reverseBetween(head.Next, left-1, right-1)
    return head
}

Key Takeaways

The crucial insight is defining the recursion clearly: reverseN handles the pure reversal of the first n nodes, while reverseBetween locates the correct starting point and stitches the reversed segment back into the original list. Understanding how successor preserves the node after the reversed part prevents loss of the tail segment.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GoLeetCodeRecursionlinked list
Nullbody Notes
Written by

Nullbody Notes

Go backend development, learning open-source project source code together, focusing on simplicity and practicality.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.