Leetcode problems for practicing recursion

James S. Plank
Thu Nov 20 16:26:45 EST 2025

I'm sorry that I have not done full writeups for these, but I'll give you partial writeups and solutions:


Leetcode 231: Power of Two

https://leetcode.com/problems/power-of-two/description

You can solve this one easily without recursion, but it's good practice with recursion as it's really simple. Think -- what's my base case? Then, how can I reduce it to a recursive call to a smaller number?

Give it a try, and if you get stuck, here's a simple recursive solution.

If you want another that's pretty much just like this, try 326 - Power of Three.


Leetcode 206: Reverse Linked List

https://leetcode.com/problems/reverse-linked-list

This is nice as it can give you practice with both recursion and linked lists.

As always with recursion, think about the base case: When the list has zero nodes? When the list has one node?

Now, think about when the list has more than one node. If you call the method recursively on the second node, how do you put the first node at the end of this new list?

Let's ASCII art that first example. When you make the recursive call, you'll be left with:

rv -> 5 -> 4 -> 3 -> 2-> nullptr.

Let me give you a suggestion -- have two variables:

Can you make it work? My solution is here.

Leetcode 3304: Find the K-th Character in String Game I

https://leetcode.com/problems/find-the-k-th-character-in-string-game-i/description.

Given the problem's constraints, you can solve it by simply building the string until it is big enough, and then returning the answer.

But that doesn't give you practice with recursion.

First, also when you're going through the problem, make sure you realize that k is one-indexed. The first letter is k=1; the second is k=2 and so on. That's a pain.

This one is a little tricky, but let me help you think about the recursion. For this one, I created a recursive method:

char kc(int k, int size);

This says, what's the answer for k when the size of the word is size? We're going to call this initially as kc(k, size), where size is a power of two greater than k. That means that when we call kc(k,size), we know the word is big enough.

We're never creating the word, btw. I just find it easier to think about by having size in the method.

Ok, now think about base cases. When k is one, then we know the answer -- 'a'. So that's our base case.

Now, what happens when k is in the first half of the word? Then we don't need the second half of the word. So we can make a recursive call to kc(k,size/2).

Ok, so now we know that k is in the second half of the word. It is going to equal its value in the first half of the word, incremented by one. So we can make a recursive call to kc(k-size/2,size), and add one to the result (wrapping around at 'z').

Is that it? Yes, that's it! Let's walk through the example of k = 10. We know from the writeup, that the answer is 'c'. We'll start with size equaling 16, since that is the smallest power of two greater than k:

- kc(10,16) calls kc(2,16), and adds one to the answer.
  - kc(2,16) returns kc(2,8), since k is in the first half of the word.
    - kc(2,8) returns kc(2,4), since k is in the first half of the word.
      - kc(2,4) returns kc(2,2), since k is in the first half of the word.
        - kc(2,2) calls kc(1,2), and adds one to the answer.
          - kc(1,2) returns 'a', since k is 1.
        - kc(2,2) returns 'b', since kc(1,2) returned 'a'.
      - kc(2,4) returns 'b'
    - kc(2,8) returns 'b'
  - kc(2,16) returns 'b'
- kc(10,16) returns 'c', since kc(2,16) returned 'b'.
My solution is here.