Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
def insert_at_pos(head, data, position):
# edge case: check if pos is 0
new_node = Node(data)
if position is 0: = head
head = new_node
return head
pos = 0
new_node = Node(data)
curr_node = head
# iterating till the position is reached
while and pos < position - 1:
pos += 1
curr_node =
# making new node's next as current node's next =
# making current node's next point to new node = new_node
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment