- 1Singly Linked List Introduction
- 2Singly Linked List – Create Operation
- 3Singly Linked List – Insert Operation
- 4Singly Linked List – Delete Operation
- 5Singly Linked List – Traversal Operation
- 6Singly Linked List – Search Operation
- 7Singly Linked List – Get Length Operation
- 8Singly Linked List – Reverse Operation
- 9Doubly Linked List Introduction
- 10Doubly Linked List – Create Operation
- 11Doubly Linked List – Insert Operation
- 12Doubly Linked List – Delete Operation
- 13Doubly Linked List – Traversal Operation
- 14Doubly Linked List – Search Operation
- 15Doubly Linked List – Get Length Operation
- 16Doubly Linked List – Reverse Operation
Singly Linked List – Create Operation
Creating a Singly Linked List
Creating a singly linked list involves building it node by node. Each node holds a value and a pointer to the next node. The last node points to null
.
Unlike arrays, linked lists grow dynamically as we insert nodes — there is no need for pre-allocated size.
Visual Example
Let’s create a singly linked list by inserting these values one at a time: 10
, 20
, 30
, 40
.
Each value will be added to the end of the list, forming a chain where each node points to the next one.
Step-by-Step Construction
-
Step 1: Create an empty list
We start with no elements in our list. Thehead
pointer isnull
, indicating the list is empty. -
Step 2: Insert
10
– It becomes the head.
This is our first node. Since the list is empty,10
is inserted and becomes the head node of the list. -
Step 3: Insert
20
– Append after10
.
We create a new node with value20
. The current tail is10
, so we update itsnext
pointer to point to20
. -
Step 4: Insert
30
– Append after20
.
Again, we create a new node with value30
. We find the last node (currently20
) and update itsnext
to point to30
. -
Step 5: Insert
40
– Append after30
.
Finally, we create the node40
and connect it to the end of the list by updating thenext
of30
.
Intuition Behind the Steps
- A singly linked list is a series of nodes where each node holds a value and a reference (or pointer) to the next node.
- We build it one node at a time by finding the last node (tail) and attaching the new node at the end.
- The first node is special—it becomes the head of the list and serves as the entry point.
Complete Pseudocode: Create Linked List by Appending
This pseudocode builds a singly linked list by adding each new node to the end of the list.
function createLinkedList():
head = null
tail = null
while input has values:
value = next input
node = new ListNode(value)
if head == null:
head = node
tail = node
else:
tail.next = node
tail = node
return head
Let’s walk through each part of the code to understand what it does.
function createLinkedList():
head = null
tail = null
Explanation: We start with an empty linked list. The head
will point to the first node, and tail
will point to the last node. Initially, both are set to null
because the list is empty.
while input has values:
value = next input
node = new ListNode(value)
Explanation: We loop through the given input values (e.g., [10, 20, 30, 40]). For each value, we create a new node using new ListNode(value)
. This node will hold the data and a pointer to the next node (initially null).
if head == null:
head = node
tail = node
Explanation: If the list is still empty (head
is null), then this is the very first node we're adding. So, we set both head
and tail
to point to this new node. The head
keeps track of where the list starts, and tail
helps us keep track of the end.
else:
tail.next = node
tail = node
Explanation: If the list already has at least one node, then we append the new node to the end. This is done by setting tail.next
to point to the new node (i.e., connect the last node to the new one), and then updating tail
to be the new node (i.e., the new node becomes the last node).
return head
Explanation: After all nodes have been added, we return head
, which points to the first node in the linked list. From head
, we can traverse the entire list by following the next
pointers.
Linked List Creation Examples
The following examples show how nodes are added one by one to build a singly linked list dynamically.
Insert Sequence | Final Linked List | Explanation |
---|---|---|
[10, 20, 30] | 10 → 20 → 30 → null | Three nodes are inserted one after another. Each new node is linked to the previous one using the next pointer. |
[5] | 5 → null | Only one node is inserted. It becomes both the head and the tail of the list. |
[] | null | No values are provided, so the list remains empty with head = null . |
[1, 2, 3, 4, 5] | 1 → 2 → 3 → 4 → 5 → null | Five nodes are inserted sequentially to build a full list. Each node points to the next until the last one, which points to null . |
Time Complexity of Singly Linked List Create Operation
Creation Case | Time Complexity | Explanation |
---|---|---|
Creating list by appending with tail pointer | O(n) | Each new node is added in constant time. Creating a list of n nodes takes linear time overall. |
Creating list by inserting at head | O(n) | Each new node is prepended in constant time. Total time grows linearly with the number of elements. |
Space Complexity of Singly Linked List Create Operation
Creation Case | Space Complexity | Explanation |
---|---|---|
Creating a list of n nodes | O(n) | Each node takes space for its value and a pointer to the next node, resulting in linear space usage. |
Key Takeaways
- Singly linked lists grow dynamically by linking nodes one-by-one.
- Use a
tail
pointer to insert efficiently at the end. - The last node always points to
null
. - Head is needed to return the complete list.
Comments
Loading comments...