Traversal in Singly Linked List Searching in Singly Linked List Finding Length in Singly Linked List
int findLength(Node* head)
void traverse(Node* head) bool search(struct Node* head, int target)
{
{ { // Initialize a counter for the length
Node* temp = head; while (head != nullptr) { int length = 0;
if (head->data == target) {
while (temp != nullptr) { // Start from the head of the list
return true; Node* current = head;
cout << temp->data << " "; }
temp = temp->next; head = head->next;
// Traverse the list and increment the length for each node
while (current != nullptr) {
} } length++;
current = current->next;
} return false; // Value not found }
} return length;
}
Insertion in Singly Linked List
at the beginning at the end at a specific position
Node* insertPos(Node* head, int pos, int data)
Node* insertAtBeginning(Node* head, int value) Node* insertAtEnd(Node* head, int value)
{
{
{ Node* newNode = new Node(value);
if (pos < 1) {
cout << "Invalid position!" << endl;
Node* newNode = new Node(value); // If the list is empty, make the new node the head return head;
newNode->next = head; if (head == nullptr) }
return newNode;
head = newNode; // Special case for inserting at the head
return head; // Traverse the list until the last node is reached
if (pos == 1) {
Node* temp = new Node(data);
} Node* curr = head; temp->next = head;
while (curr->next != nullptr) { return temp;
curr = curr->next; }
}
// Traverse the list to find the node before the insertion point
Node* prev = head;
// Link the new node to the current last node int count = 1;
curr->next = newNode; while (count < pos - 1 && prev != nullptr) {
return head; prev = prev->next;
count++;
}
}
// If position is greater than the number of nodes
if (prev == nullptr) {
cout << "Invalid position!" << endl;
return head;
}
// Insert the new node at the specified position
Node* temp = new Node(data);
temp->next = prev->next;
prev->next = temp;
return head;
}
Deletion in Singly Linked List
at the beginning at the end at a specific position
Node* removeFirstNode(Node* head) Node* removeLastNode(Node* head) Node* deleteAtPosition(Node* head, int position)
{ { {
// If the list is empty or the position is invalid
if (head == nullptr) if (head == nullptr) if (head == nullptr || position < 1) {
return nullptr; return nullptr; return head;
Node* temp = head; }
head = head->next; if (head->next == nullptr) {
delete head; // If the head needs to be deleted
if (position == 1) {
delete temp; return nullptr; Node* temp = head;
} head = head->next;
return head; delete temp;
} // Find the second last node return head;
}
Node* second_last = head;
while (second_last->next->next != nullptr) // Traverse to the node before the position to be deleted
second_last = second_last->next; Node* current = head;
for (int i = 1; i < position - 1 && current != nullptr; i++) {
// Delete last node current = current->next;
}
delete (second_last->next);
// If the position is out of range
// Change next of second last if (current == NULL || current->next == nullptr) {
second_last->next = nullptr; return;
}
return head;
// Store the node to be deleted
} Node* temp = current->next;
// Update the links to bypass the node to be deleted
current->next = current->next->next;
// Delete the node
delete temp;
return head;
}