 # Can't understand how recursion works

``````import LinkedList

# Definition for singly-linked list node.
# class ListNode:
#     def __init__(self, value, next_node=None):
#         self.value = value
#         self.next_node = next_node

# define remove_node() here
def remove_node(head, i):
if i < 0:
return head
if head is None:
return None
if i == 0:
return head.next_node
head.next_node = remove_node(head.next_node, i - 1)
return head

# Test code - do not edit
gemstones = LinkedList.LinkedList(["Amber", "Sapphire", "Jade", "Pearl"])
head = remove_node(gemstones.head, 2)
print(head.flatten())
``````

I just can’t seem to understand the recursive strategy behind this approach. This is the code for removing the i-th node from a linked list. Can someone explain this code please?

Is it the recursion that’s throwing you off or the linked-list bit?

Here’s a simpler example without linked lists

``````def fill(a, char, i):
a[i].append(char)
if i == 0:
return
fill(a, char, i-1)

a = [[],[],[]]
fill (a, "x", 2)

#result: [['x'], ['x'], ['x']]
``````

You might know this already but the common math example is fibonacci. Where the value of the i’th term is determined by the the sum of `i-2` and `i-1`. Or even calculating towers of hanoi has a recursive solution as well, if you know that game.

Recursion just takes time and practice. You might have seen coding challenges to draw patterns with stars while learning for-loops, I think these types of problems are great for recursion as well.

Here’s a link to some: printing stars in pyramid shape python for loop - Google Search

Particularly good ones are things like this and similar ones…
might be tough at first…

``````*     *
*   *
* *
*
* *
*   *
*     *
``````

Once you call `head.next_node = remove_node(head.next_node, i - 1)` with `i == 0`, it returns head.next_node ending the recursive calling and all other calls return head, maintaining the pointers for the rest of the list.