Skip to main content
C Programming Basics
CHAPTER 22 Beginner

Linked Lists in C

Updated: May 17, 2026
5 min read

# CHAPTER 22

Linked Lists in C

1. Introduction

Arrays are great, but their size is fixed. Even with malloc, resizing an array requires copying all elements to a new location. A Linked List solves this. It is a dynamic data structure where elements (Nodes) are scattered throughout memory and linked together using pointers.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain what a Node is.
  • Understand the difference between an Array and a Linked List.
  • Create and link Nodes dynamically.
  • Traverse a Linked List.
  • Implement basic insertion.

3. What is a Node?

A Linked List consists of Nodes. Every Node has two parts:
  1. 1. Data: The actual value being stored (e.g., an integer).
  1. 2. Next Pointer: A pointer holding the memory address of the *next* Node in the list.
c
1234
struct Node {
    int data;
    struct Node *next; // Pointer to a struct of the same type!
};

4. Creating a Linked List

Let's create a list with three nodes: Head -> Node1 -> Node2 -> NULL.
c
12345678910111213141516171819202122232425262728293031
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

int main() {
    // 1. Declare pointers for the nodes
    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *third = NULL;

    // 2. Allocate memory in the Heap
    head = (struct Node*) malloc(sizeof(struct Node));
    second = (struct Node*) malloc(sizeof(struct Node));
    third = (struct Node*) malloc(sizeof(struct Node));

    // 3. Assign data and link them
    head->data = 10;
    head->next = second; // Link first to second

    second->data = 20;
    second->next = third; // Link second to third

    third->data = 30;
    third->next = NULL; // Mark the end of the list

    return 0;
}

5. Visualizing the Linked List

Unlike an array, these nodes can be anywhere in RAM!
1234567
HEAD (ptr) 
   |
   v
+------+---------+      +------+---------+      +------+------+
| 10   | 0x500   | ---> | 20   | 0x800   | ---> | 30   | NULL |
+------+---------+      +------+---------+      +------+------+
 Address: 0x200          Address: 0x500          Address: 0x800

6. Traversing the List

To print all elements, we start at the head and follow the next pointers until we hit NULL.
c
123456789
void printList(struct Node *n) {
    while (n != NULL) {
        printf("%d -> ", n->data);
        n = n->next; // Move to the next node
    }
    printf("NULL\n");
}
// Call inside main: printList(head);
// Output: 10 -> 20 -> 30 -> NULL

7. Inserting a Node at the Beginning

To add a new node at the front, we create it, point its next to the current head, and then update head to be the new node.
c
12345678910111213141516
// Note: We pass a pointer to a pointer (**head_ref) because we are changing where 'head' points!
void insertAtFront(struct Node **head_ref, int new_data) {
    // 1. Allocate new node
    struct Node *new_node = (struct Node*) malloc(sizeof(struct Node));
    
    // 2. Put in the data
    new_node->data = new_data;
    
    // 3. Make next of new node as head
    new_node->next = (*head_ref);
    
    // 4. Move the head to point to the new node
    (*head_ref) = new_node;
}

// Call inside main: insertAtFront(&head, 5);

8. Array vs. Linked List

FeatureArrayLinked List
MemoryContiguousScattered
SizeFixedDynamic
Insertion/DeletionSlow O(n) (shifting elements)Fast O(1) (just change pointers)
AccessFast O(1) (index)Slow O(n) (must traverse)

9. Common Mistakes

  • Losing the Head: If you do head = head->next; to traverse the list, you have permanently lost the start of your list (Memory Leak!). Always use a temporary pointer struct Node *temp = head; for traversal.
  • Forgetting NULL: If the last node's next pointer isn't set to NULL, traversal loops will run off into garbage memory and crash.
  • Not passing pointer-to-pointer: If a function needs to change the head pointer itself, you must pass &head (a struct Node ).

10. Exercises

  1. 1. Write a function to count the number of nodes in a linked list.
  1. 2. Write a function to append a node to the END of a linked list. (Hint: you must traverse to the end first).

11. MCQ Quiz with Answers

Question 1

What are the two parts of a Linked List node?

Question 2

How is the end of a Linked List indicated?

Question 3

Which memory region holds dynamically created linked list nodes?

Question 4

What is the time complexity of accessing the 100th element in a Linked List?

Question 5

Why use a Linked List over an Array?

Question 6

When inserting a node at the head, what is passed to the function?

Q7. Does a Linked List use contiguous memory? a) Yes b) No Answer: b) No
Question 8

What happens if a node's next points to a previous node?

Question 9

Which operator is used to access members of a Node pointer?

Question 10

What is a Memory Leak in a linked list context?

12. Interview Questions

  • Q: Explain the difference between Singly, Doubly, and Circular linked lists.
  • Q: How would you detect a cycle (infinite loop) in a linked list? (Hint: Floyd's Tortoise and Hare algorithm).
  • Q: Write code to reverse a linked list.

13. Summary

Linked Lists are dynamic data structures made of Nodes scattered across the Heap. Each node holds data and a pointer to the next node. While slower to access elements than an array, they excel at dynamic sizing and rapid insertions/deletions.

14. Next Chapter Recommendation

In Chapter 23: Stack and Queue Implementation, we will use our knowledge of Arrays and Linked Lists to build two fundamental data structures used everywhere in computer science.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·