Linked List in C (original) (raw)
Last Updated : 24 Jan, 2026
A linked list is a linear data structure made of nodes connected using pointers. Each node has: Data: The value stored in the node. Pointer: A reference to the next node.
- Nodes are not stored in contiguous memory like arrays; they can be anywhere in memory.
- To access a node, we start from the head and traverse sequentially through the list C++ `
#include <stdio.h> #include <stdlib.h>
// Define the node structure struct Node { int data; struct Node *next; };
int main() { // Create nodes struct Node *head = NULL; struct Node *second = NULL; struct Node *third = NULL;
// Allocate memory
head = (struct Node *)malloc(sizeof(struct Node));
second = (struct Node *)malloc(sizeof(struct Node));
third = (struct Node *)malloc(sizeof(struct Node));
// Check allocation
if (!head || !second || !third) {
printf("Memory allocation failed\n");
return 1;
}
// Assign data and link nodes
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = NULL;
// Print linked list
struct Node *temp = head;
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
// Free allocated memory
temp = head;
while (temp != NULL)
{
struct Node *next = temp->next;
free(temp);
temp = next;
}
return 0;}
`
Output
10 -> 20 -> 30 -> NULL
Types of Linked List in C
Linked list can be classified on the basis of the type of structure they form as a whole and the direction of access. Based on this classification, there are five types of linked lists:
Singly Linked List in C
- A linked list or singly linked list is a linear data structure that is made up of a group of nodes in which each node has two parts: the data, and the pointer to the next node.
- The last node's (also known as tail) pointers point to NULL to indicate the end of the linked list.

Doubly Linked List
- A doubly linked list is a bit more complex than singly linked list. In it, each node contains three parts: the data, a pointer to the next node, and one extra pointer which points to the previous node.
- This allows for traversal in both directions, making it more versatile than a singly linked list.

Basic Operations on Doubly Linked List
| Operation | Operation Type | Description | Time Complexity | Space Complexity |
|---|---|---|---|---|
| Insertion | At Beginning | Insert a new node at the start of a linked list. | O (1) | O (1) |
| At the End | Insert a new node at the end of the linked list. | O (N) | O (1) | |
| At Specific Position | Insert a new node at a specific position in a linked list. | O (N) | O (1) | |
| Deletion | From Beginning | Delete a node from the start of a linked list | O (1) | O (1) |
| From the End | Delete a node at the end of a linked list. | O (N) | O (1) | |
| A Specific Node | Delete a node from a specific position of a linked list. | O (N) | O (1) | |
| Traversal | Traverse the linked list from start to end or vice versa. | O (N) | O (1) |
Circular Linked List
A circular linked list is a variation of a singly linked list where the last node points back to the first node, forming a circle. This means there is no NULL at the end, and the list can be traversed in a circular manner.

**Note: A circular linked list can also be represented by a pointer to the last node.
Basic Operations on Circular Linked List
| Operation | Operation Type | Description | Time Complexity | Space Complexity |
|---|---|---|---|---|
| Insertion | At Beginning | Insert a new node at the start of a linked list. | O (N) | O (1) |
| At the End | Insert a new node at the end of the linked list. | O (N) | O (1) | |
| At Specific Position | Insert a new node at a specific position in a linked list. | O (N) | O (1) | |
| Deletion | From Beginning | Delete a node from the start of a linked list | O (N) | O (1) |
| From the End | Delete a node at the end of a linked list. | O (N) | O (1) | |
| A Specific Node | Delete a node from a specific position of a linked list. | O (N) | O (1) | |
| Traversal | Traverse the linked list from start to end or vice versa. | O (N) | O (1) |
Memory Management in Linked List
Memory management is the process of efficiently allocating, using, and freeing memory in a program so that the system runs smoothly without wasting resources or causing errors.
Dynamic Memory Allocation using malloc().
- In C, linked list nodes are not created automatically like array elements; their memory must be allocated manually.
- malloc() is used to request memory from the heap at runtime. For example :
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
- This allows the linked list to grow or shrink dynamically, unlike arrays which have fixed size.
- If malloc() is not used, we would have to define a fixed-size array of nodes, which is less flexible.
Dynamic Memory Allocation using calloc().
- A function in C used to allocate memory dynamically for nodes at runtime. Unlike malloc(), it initializes all allocated memory to zero.
- It ensures that pointer fields like next are automatically NULL, reducing errors, useful when creating multiple nodes at once. For example:
struct Node* newNode = (struct Node*)calloc(1, sizeof(struct Node));
Freeing Memory with free().
- When a node is deleted from the linked list, the memory allocated to it must be released using free().
- For example :
free(nodeToDelete);
nodeToDelete = NULL;
- If we don’t free memory, it stays allocated even though it’s no longer used, causing a memory leak.
Applications of Linked Lists
The following are some major applications of linked list:
- Dynamic memory allocation efficiently manages and allocates dynamic memory in systems and applications.
- Implementing other data structures such as stacks, queues, etc.
- Represents and manipulates polynomials, with each node storing terms.
- Used in file system management dynamically in operating systems.
Advantages of a Linked List
The following points shows the advantages of linked list:
- Linked lists can grow or shrink in size dynamically, as memory is allocated or deallocated as needed.
- Inserting or deleting nodes in a linked list is efficient and does not require shifting elements, unlike arrays.
- Memory is utilized more efficiently as linked lists do not require a pre-allocated size, reducing wasted space.
- They serve as the foundation for implementing more complex data structures like stacks, queues, and graphs.
- They can utilize non-contiguous memory blocks, making them suitable for applications where memory is fragmented.
Disadvantages of a Linked List
Disadvantages of linked list are mentioned below:
- Each node requires extra memory for storing a pointer.
- Linked lists do not allow direct access to elements by index. Accessing a specific node requires traversing from the head, leading to slower access times.
- Managing pointers can be tricky, increasing the complexity of coding.
- Searching for an element or accessing a node by index takes O(N) time, making linked lists slower for such operations compared to arrays.
- Non-contiguous memory allocation results in more cache misses.
- Singly linked lists do not support easy backward traversal.