Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Visualizer › Linked List

Linked List Insertion

Inserts a new node at a position: walk to the node before it, point the new node to the next one, then link the previous node to the new node.

How it works

Complexity

Code (Python)

def insert_at(head, value, pos):
    node = Node(value)
    if pos == 0:
        node.next = head
        return node
    temp = head
    for _ in range(pos - 1):
        temp = temp.next
    node.next = temp.next
    temp.next = node
    return head

Practise: Linked List problems