Singly Linked List Tutorial (original) (raw)
Last Updated : 26 Feb, 2025
A **singly linked list is a fundamental data structure, it consists of **nodes where each node contains a **data field and a **reference to the next node in the linked list. The next of the last node is **null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations.
Understanding Node Structure
In a singly linked list, each node consists of two parts: data and a pointer to the next node. This structure allows nodes to be dynamically linked together, forming a chain-like sequence.
C++ `
// Definition of a Node in a singly linked list struct Node {
// Data part of the node
int data;
// Pointer to the next node in the list
Node* next;
// Constructor to initialize the node with data
Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
C
// Definition of a Node in a singly linked list
struct Node {
int data;
struct Node* next;
};
// Function to create a new Node struct Node* newNode(int data) { struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data = data; temp->next = NULL; return temp; }
Java
// Definition of a Node in a singly linked list public class Node { int data; Node next;
// Constructor to initialize the node with data
public Node(int data)
{
this.data = data;
this.next = null;
}
}
Python
Definition of a Node in a singly linked list
class Node:
def init(self, data):
# Data part of the node
self.data = data
self.next = None
JavaScript
// Definition of a Node in a singly linked list
class Node {
constructor(data) {
// Data part of the node
this.data = data;
this.next = null;
}
}
`
In this example, the Node class contains an integer data field (**data) to store the information and a pointer to another Node (**next) to establish the link to the next node in the list.
**1. Traversal of Singly Linked List
Traversal in a linked list means visiting each node and performing operations like printing or processing data.
**Step-by-step approach:
- **Initialize a pointer (
current
) to the head of the list. - **Loop through the list using a
while
loop untilcurrent
becomesNULL
. - **Process each node (e.g., print its data).
- **Move to the next node by updating
current = current->next
.
To read more about Traversal Operation in linked list Refer, Traversal of Singly Linked List
2. Searching in Singly Linked List
Searching in a Singly Linked List refers to the process of looking for a specific element or value within the elements of the linked list.
**Step-by-step approach:
- **Start from the head of the linked list.
- **Check each node’s data:
- If it matches the target value, **return true (element found).
- Otherwise, move to the next node.
- **Repeat until the end (
NULL
) is reached. - If no match is found, **return false.
To read more about Searching Operation in linked list Refer, Search an element in a Linked List
3. Length of Singly Linked List
Finding the length of a **Singly Linked List means counting the total number of nodes.
**Step-by-step approach:
- **Initialize a counter (
length = 0
). - **Start from the head, assign it to
current
. - **Traverse the list:
- **Increment
length
for each node. - **Move to the next node (
current = current->next
).
- **Increment
- **Return the final
length
whencurrent
becomesNULL
.
To read more about Finding Length of linked list Refer, Find Length of a Linked List
4. Insertion in Singly Linked List
Insertion is a fundamental operation in linked lists that involves adding a new node to the list. There are several scenarios for insertion:
**a. Insertion at the Beginning of Singly Linked List: Insertion at the beginning involves adding a new node before the current head, making it the new head.
Insert a Node at the Front/Beginning of Linked List
**Step-by-step approach:
- Create a new node with the given value.
- Set the **next pointer of the new node to the current head.
- Move the head to point to the new node.
- Return the new head of the linked list.
To read more about Insertion at the Beginning of linked list Refer, Insert a Node at Front/Beginning of a Linked List
**b. Insertion at the End of Singly Linked List: To insert a node at the end of the list, traverse the list until the last node is reached, and then link the new node to the current last node
Insertion at end of Linked List
**Step-by-step approach:
- Create a new node with the given value.
- Check if the list is empty:
- If it is, make the new node the head and return.
- Traverse the list until the last node is reached.
- Link the new node to the current last node by setting the last node's next pointer to the new node.
To read more about Insertion at the end of linked list Refer, Insert Node at the End of a Linked List
**c. Insertion at a Specific Position of the Singly Linked List: To insert a node at a specific position, traverse the list to the desired position, link the new node to the next node, and update the links accordingly.
**Step-by-step approach:
- **Create a new node and assign it a value.
- **If inserting at the beginning (position = 1):
- Point the new node’s
next
to the current head. - Update the head to the new node.
- **Return (Insertion done).
- Point the new node’s
- **Otherwise, traverse the list:
- Start from the head and move to the
(position - 1)ᵗʰ
node (just before the desired position). - If the position is beyond the list length, **return an error or append at the end.
- Start from the head and move to the
- **Insert the new node:
- Point the new node’s
next
to the next node of the current position. - Update the previous node’s
next
to the new node.
- Point the new node’s
- **Return the updated list.
To read more about Insertion at the specific position of linked list Refer, Insert a node at a specific position in a linked list
5. Deletion in Singly Linked List
Deletion involves removing a node from the linked list. Similar to insertion, there are different scenarios for deletion:
**a. Deletion at the Beginning of Singly Linked List: To delete the first node, update the head to point to the second node in the list.
Deletion at beginning in a Linked List
**Steps-by-step approach:
- Check if the head is **NULL.
- If it is, return **NULL (the list is empty).
- Store the current head node in a temporary variable **temp.
- Move the head pointer to the next node.
- Delete the temporary node.
- Return the new head of the linked list.
To read more about Deletion at the beginning in linked list Refer, Deletion at beginning (Removal of first node) in a Linked List
**b. Deletion at the End of Singly Linked List: To delete the last node, traverse the list until the second-to-last node and update its next field to None.
Deletion at the end of linked list
**Step-by-step approach:
- Check if the head is **NULL.
- If it is, return NULL (the list is empty).
- Check if the head's **next is **NULL (only one node in the list).
- If true, delete the head and return **NULL.
- Traverse the list to find the second last node (**second_last).
- Delete the last node (the node after **second_last).
- Set the **next pointer of the second last node to **NULL.
- Return the head of the linked list.
To read more about Deletion at the end in linked list Refer, Deletion at end (Removal of last node) in a Linked List
**c. Deletion at a Specific Position of Singly Linked List: To delete a node at a specific position, traverse the list to the desired position, update the links to bypass the node to be deleted.
Delete a Linked List node at a given position
**Step-by-step approach:
- Check if the list is empty or the position is invalid, return if so.
- If the head needs to be deleted, update the head and delete the node.
- Traverse to the node before the position to be deleted.
- If the position is out of range, return.
- Store the node to be deleted.
- Update the links to bypass the node.
- Delete the stored node.
To read more about Deletion at the specific position in linked list Refer, Delete a Linked List node at a given position
6. Modify a Singly Linked List
Updating in a Singly Linked List means modifying the value of a node at a given position.
**Step-by-step approach:
- **Start from the head of the list.
- **Traverse to the required position (move
current
node toposition
). - **Check if the position is valid:
- If the position is out of bounds, **return an error.
- **Update the node’s data with the new value.
- **Return the modified list.
To read more about Updation Operation in Linked List Refer, Modify contents of Linked List
7. Reversing a Singly Linked List
Reversing a singly linked list means changing the direction of pointers so that the last node becomes the new head.
**Step-by-step approach:
- **Initialize three pointers:
prev = NULL
(to track the previous node)current = head
(starting point)next = NULL
(to store the next node temporarily)
- **Iterate through the list:
- Store
next = current->next
(save next node). - Reverse the link:
current->next = prev
. - Move
prev
andcurrent
forward (prev = current
,current = next
).
- Store
- **Update
head
toprev
(new head is the last node).
To read more about Reversal Operation in Linked List Refer, Reverse a Linked List