Unit 5 of 14 · Linked Lists

Insert

1 min Updated Jul 2026

Start with an empty list. Insert three values at the end, one at a time: 10, 20, 30.

def insert(head, data):
    newNode = Node(data)

    # If the list is empty
    if head is None:
        return newNode

    currentNode = head

    # Traverse to the last node
    while currentNode.next:
        currentNode = currentNode.next

    # Insert the new node
    currentNode.next = newNode

    return head

Then, you can use the method as

head = None
head = insert(head, 10)
head = insert(head, 20)
head = insert(head, 30)