educative.io

Confusion in insertion method of Linked List

In the below line of code i did not understand how does the first input to method i.e.,llist.head.next leads us to the previous node
llist.insert_after_node(llist.head.next,“D”)

The function insert_after_node takes two arguments; the first being the location of the previous node, and the second being the data to be placed after this node referenced in the first argument.

In the case of the line you provided, we want a new node with data element “D” to be placed after the node that comes after the head in the list. Or, equivalently, this would indicate that we want to place a node with data “D” after the second node.

2 Likes