Data Structures - Linked List
A linked list (Linked List) is a linear data structure formed by connecting several nodes in sequence via pointers.
Unlike arrays, the elements of a linked list are not required to be stored contiguously in memory.
Basic concepts of linked lists and node structure
Each node of a linked list contains two parts: a data field (data) that stores the actual data, and a pointer field (next) that points to the next node.
The figure above shows the actual appearance of a linked list in memory. Each node is independently allocated on the heap via malloc, so they need not be contiguous in memory. Nodes are chained through next pointers, and the last node's next pointing to NULL marks the end of the list.
In C, a linked list node is defined via a struct:
Example
#include
#include
/* linked list node struct
data: data field, stores integer data
next: pointer field, points to the next node of the same type */
struct
Node
{
int
data
;
struct
Node
next
;
}
;
/* create a new node
parameter value: the node's data value
return value: a pointer to the new node; returns NULL if allocation fails */
struct
Node
createNode
(
int
value
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
if
(
newNode
==
NULL
)
{
printf
(
"Memory allocation failed\n"
)
;
return
NULL
;
}
newNode
->
data
=
value
;
/* set the data */
newNode
->
next
=
NULL
;
/* initialize the new node's next to NULL */
return
newNode
;
}
struct Node* nextis a self-referential structure, meaning the struct contains a pointer to another struct of the same type. This is the core technique for building all chained data structures (linked lists, trees, graphs).
Basic operations on a singly linked list
Creating a linked list and traversal
Example
#include
#include
struct
Node
{
int
data
;
struct
Node
next
;
}
;
/* traverse and print all nodes of the linked list
starting from the head node, visit each node along the next pointer until NULL */
void
traverse
(
struct
Node
head
)
{
struct
Node
current
=
head
;
/* start from the head */
printf
(
"List content: "
)
;
while
(
current
!=
NULL
)
{
printf
(
"%d -> "
,
current
->
data
)
;
current
=
current
->
next
;
/* move to the next node */
}
printf
(
"NULL\n"
)
;
}
/* create a linked list with n nodes (1, 2, 3, ..., n) */
struct
Node
createList
(
int
n
)
{
if
(
n
<=
0
)
return
NULL
;
struct
Node
head
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
head
->
data
=
1
;
head
->
next
=
NULL
;
struct
Node
tail
=
head
;
/* tail always points to the last node */
for
(
int
i
=
2
;
i
<=
n
;
i
++
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
i
;
newNode
->
next
=
NULL
;
tail
->
next
=
newNode
;
/* connect the new node to the end of the list */
tail
=
newNode
;
/* update the tail pointer */
}
return
head
;
}
int
main
(
)
{
struct
Node
head
=
createList
(
5
)
;
traverse
(
head
)
;
/* output: list contents: 1 -> 2 -> 3 -> 4 -> 5 -> NULL */
return
0
;
}
Insertion operation
The figure above shows the complete process of pointer changes during linked list insertion and deletion. Understanding the order of pointer adjustments is crucial: a wrong order will break the list or cause a memory leak.
Example
#include
#include
struct
Node
{
int
data
;
struct
Node
next
;
}
;
/* insert a new node at the head of the list (head insertion)
time complexity: O(1)
return value: the new head node pointer */
struct
Node
insertAtHead
(
struct
Node
head
,
int
value
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
value
;
newNode
->
next
=
head
;
/* the new node points to the original head node */
return
newNode
;
/* The new node becomes the new head node */
}
/* insert a new node at the tail of the list (tail insertion)
time complexity: O(n), needs to traverse to the tail */
struct
Node
insertAtTail
(
struct
Node
head
,
int
value
)
{
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
value
;
newNode
->
next
=
NULL
;
if
(
head
==
NULL
)
{
return
newNode
;
/* empty list, the new node is the head node */
}
struct
Node
current
=
head
;
while
(
current
->
next
!=
NULL
)
{
current
=
current
->
next
;
/* traverse to the last node */
}
current
->
next
=
newNode
;
/* the tail node points to the new node */
return
head
;
}
/* insert a new node after a specified position
time complexity: O(n), need to find prevNode first */
void
insertAfter
(
struct
Node
prevNode
,
int
value
)
{
if
(
prevNode
==
NULL
)
{
printf
(
"Predecessor node cannot be null\n"
)
;
return
;
}
struct
Node
newNode
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
newNode
->
data
=
value
;
/* key: first make the new node point to its successor, then make the predecessor point to the new node */
newNode
->
next
=
prevNode
->
next
;
prevNode
->
next
=
newNode
;
}
void
printList
(
struct
Node
head
)
{
struct
Node
cur
=
head
;
while
(
cur
!=
NULL
)
{
printf
(
"%d "
,
cur
->
data
)
;
cur
=
cur
->
next
;
}
printf
(
"\n"
)
;
}
int
main
(
)
{
struct
Node
head
=
NULL
;
head
=
insertAtHead
(
head
,
30
)
;
/* list: 30 */
head
=
insertAtHead
(
head
,
20
)
;
/* list: 20 -> 30 */
head
=
insertAtHead
(
head
,
10
)
;
/* list: 10 -> 20 -> 30 */
printf
(
"After head insertion: "
)
;
printList
(
head
)
;
/* output: 10 20 30 */
head
=
insertAtTail
(
head
,
40
)
;
/* list: 10 -> 20 -> 30 -> 40 */
printf
(
"After tail insertion: "
)
;
printList
(
head
)
;
/* output: 10 20 30 40 */
insertAfter
(
head
->
next
,
25
)
;
/* insert 25 after 20 */
printf
(
"After middle insertion: "
)
;
printList
(
head
)
;
/* output: 10 20 25 30 40 */
return
0
;
}
Delete operation
Example
#include
#include
struct
Node
{
int
data
;
struct
Node
next
;
}
;
/* delete the head node of the list
time complexity: O(1)
return value: the new head node pointer */
struct
Node
deleteHead
(
struct
Node
head
)
{
if
(
head
==
NULL
)
return
NULL
;
struct
Node
temp
=
head
;
/* save the old head node */
head
=
head
->
next
;
/* move the head pointer forward */
free
(
temp
)
;
/* free the old head node's memory */
return
head
;
}
/* delete the first node whose value equals target in the list
time complexity: O(n), need to traverse to find the target node */
struct
Node
deleteByValue
(
struct
Node
head
,
int
target
)
{
if
(
head
==
NULL
)
return
NULL
;
/* special case: the target value is in the head node */
if
(
head
->
data
==
target
)
{
struct
Node
temp
=
head
;
head
=
head
->
next
;
free
(
temp
)
;
return
head
;
}
/* traverse to find the target node's predecessor */
struct
Node
current
=
head
;
while
(
current
->
next
!=
NULL
&&
current
->
next
->
data
!=
target
)
{
current
=
current
->
next
;
}
/* found the target node, delete it */
if
(
current
->
next
!=
NULL
)
{
struct
Node
temp
=
current
->
next
;
current
->
next
=
current
->
next
->
next
;
/* skip the target node */
free
(
temp
)
;
/* free the target node's memory */
}
return
head
;
}
void
printList
(
struct
Node
head
)
{
struct
Node
cur
=
head
;
while
(
cur
!=
NULL
)
{
printf
(
"%d "
,
cur
->
data
)
;
cur
=
cur
->
next
;
}
printf
(
"\n"
)
;
}
int
main
(
)
{
/* build list: 10 -> 20 -> 30 -> 40 */
struct
Node
head
=
NULL
;
int
vals
[
]
=
{
10
,
20
,
30
,
40
}
;
for
(
int
i
=
3
;
i
=
0
;
i
--
)
{
struct
Node
n
=
(
struct
Node
)
malloc
(
sizeof
(
struct
Node
)
)
;
n
->
data
=
vals
[
i
]
;
n
->
next
=
head
;
head
=
n
;
}
printf
(
"Original linked list: "
)
;
printList
(
head
)
;
/* output: 10 20 30 40 */
head
=
deleteHead
(
head
)
;
printf
(
"After deleting head: "
)
;
printList
(
head
)
;
/* output: 20 30 40 */
head
=
deleteByValue
(
head
,
30
)
;
printf
(
"After deleting 30: "
)
;
printList
(
head
)
;
/* output: 20 40 */
return
0
;
}
In a linked list's deletion operation,
free()is crucial. If you only adjust pointers without freeing the deleted node's memory, it causes a memory leak. In C, everymallocmust have a correspondingfree.
Linked list vs array comparison
| Comparison Dimensions | arrays | Linked list |
|---|---|---|
| Memory layout | Contiguous storage | Scattered storage, connected via pointers |
| random access | O(1) | O(n) |
| Head insertion/deletion | O(n) | O(1) |
| Tail insertion/deletion | O(1) | O(1) (with tail pointer) / O(n) (without tail pointer) |
| Middle insertion/deletion | O(n) | O(1) (known position) |
| Space overhead | Data itself only | Data + pointer (each node adds 8 bytes (64-bit)) |
| Cache friendliness | High (spatial locality) | Low (nodes scattered) |
| Capacity | Fixed (static array) | dynamic growth |
Understanding this trade-off is one of the core skills in choosing a data structure.
If the data size is known before runtime, changes infrequently, and frequent random access is needed, an array is the better choice.
If the data size changes frequently and is uncertain, a dynamic structure like a linked list is more flexible.