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 withmalloc, 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. Data: The actual value being stored (e.g., an integer).
- 2. Next Pointer: A pointer holding the memory address of the *next* Node in the list.
c
4. Creating a Linked List
Let's create a list with three nodes:Head -> Node1 -> Node2 -> NULL.
c
5. Visualizing the Linked List
Unlike an array, these nodes can be anywhere in RAM!6. Traversing the List
To print all elements, we start at thehead and follow the next pointers until we hit NULL.
c
7. Inserting a Node at the Beginning
To add a new node at the front, we create it, point itsnext to the current head, and then update head to be the new node.
c
8. Array vs. Linked List
| Feature | Array | Linked List |
|---|---|---|
| Memory | Contiguous | Scattered |
| Size | Fixed | Dynamic |
| Insertion/Deletion | Slow O(n) (shifting elements) | Fast O(1) (just change pointers) |
| Access | Fast 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 pointerstruct Node *temp = head;for traversal.
-
Forgetting NULL: If the last node's
nextpointer isn't set toNULL, traversal loops will run off into garbage memory and crash.
-
Not passing pointer-to-pointer: If a function needs to change the
headpointer itself, you must pass&head(astruct Node).
10. Exercises
- 1. Write a function to count the number of nodes in a linked list.
- 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?
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.