algoritmauzmanı
TREN
3.1Singly Linked List
Lesson 3.1.6

Inserting at the head

A new element can go into a linked list in three places: the front, the end or the middle. This lesson covers the shortest of the three, adding a new at the front. Inserting at the head sits underneath many other structures, such as a stack built on a linked list.

What you will learn
  • In which order the new node is linked in, and why the order matters
  • Why the operation does not depend on the length of the list, that is, why it is
  • How the same operation is written in C, Java and Python, and the most common mistake in C
4 minTime O(1)Memory O(1)C · Java · Python

1Idea

The holds the start of the list. To put a new node in front, we first point the new node's field at the old first node, then move to the new node. We never need to the rest of the list.

The order matters. If we change first, we lose the address of the old first node and can never reach the rest of the list again.

2Step by step

The drawing adds 5 to the front of a three-node list holding 12, 7 and 3. At each step the matching line of code lights up.

NULL5?0x2c1012?0x1a407?0x1a903?0x1b20headnode
  1. Start. points to the first node, which holds 12. The list has three nodes.
  2. Memory is allocated for the new node () and 5 is written into it. The node is not linked to the list yet; its next field holds nothing meaningful.
  3. The value of is written into the new node's field. The new node now points to the old first node (12).
  4. is moved to the new node. 5 is now the first element.
  5. Result: 5 → 12 → 7 → 3. None of the other nodes were touched.

struct Node {    int data;    struct Node* next;}; void insertAtHead(struct Node** head, int value) {    struct Node* node = malloc(sizeof(struct Node));    node->data = value;    node->next = *head;    *head = node;}
class Node {    int data;    Node next;    Node(int data) { this.data = data; }} Node insertAtHead(Node head, int value) {    Node node = new Node(value);    node.next = head;    return node;  // the new head}
class Node:    def __init__(self, data):        self.data = data        self.next = None def insert_at_head(head, value):    node = Node(value)    node.next = head    return node  # the new head

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

Independent of the number of nodes: one allocation and two assignments.

Memory
O(1)

Space is allocated only for the new node.

Compare: when inserting at the end without a pointer to the last node, you have to the whole list, which makes that operation .

4Common mistake

The most common mistake in C is passing by value:

void insertAtHead(struct Node* head, int value)

This function changes a copy of . The caller's still points to the old node and the new node is lost. So either pass struct Node** or return the new head. Pointers to pointers are covered in Memory, addresses and pointers.

5Try it

What happens if we insert at the head while the list is empty (head = NULL)?

Show the answer

The answer is B. The line copies the value of , which is , and you get a correct one-node list. Inserting at the head has no special case for an empty list. The reverse operation is in Deleting from the head.

If we swap the two assignments and write head = node first, then node->next = head, what happens?

Show the answer

The answer is B. After the first assignment already points to the new node. The second line writes the new node into its own field; the address of the old first node is no longer stored anywhere.

Lessons you mark show up in the curriculum. This is kept on this device only.