Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created June 17, 2020 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamsitallapudi/ffc5239865f0d14a64de2ed506732159 to your computer and use it in GitHub Desktop.
Save vamsitallapudi/ffc5239865f0d14a64de2ed506732159 to your computer and use it in GitHub Desktop.
def insert_at_pos(head, data, position):
# edge case: check if pos is 0
new_node = Node(data)
if position is 0:
new_node.next = head
head = new_node
return head
pos = 0
new_node = Node(data)
curr_node = head
# iterating till the position is reached
while curr_node.next and pos < position - 1:
pos += 1
curr_node = curr_node.next
# making new node's next as current node's next
new_node.next = curr_node.next
# making current node's next point to new node
curr_node.next = new_node
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment