Singly Linked List
Singly Linked List
// Set the next pointer of the new node to the current head
new_node->next = head;
return head;
}
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.
We mainly find the node after which we need to insert the new
node. If we encounter a NULL before reaching that node, it means
that the given position is invalid.
Below is the function for insertion at a specific position of the singly
linked list:
return head;
}
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.
o 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.
Below is the function for deletion at the beginning of singly linked
list:
return head;
}
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.
if (head->next == NULL) {
free(head);
return NULL;
}
return head;
}
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.
Below is the function for deletion at a specific position of singly
linked list:
return head;
}
What is a Doubly Linked List?
A doubly linked list is a data structure that consists of a set of nodes, each of which
contains a value and two pointers, one pointing to the previous node in the list and one
pointing to the next node in the list. This allows for efficient traversal of the list in both
directions, making it suitable for applications where
frequent insertions and deletions are required.
Doubly Linked List
Representation of Doubly Linked List in Data Structure
In a data structure, a doubly linked list is represented using nodes that have three fields:
1. Data
2. A pointer to the next node (next)
3. A pointer to the previous node (prev)
struct Node {
Approach:
To insert a new node at the beginning/front of doubly linked list, we create a new node
with its previous pointer as NULL and next pointer to the current head of the doubly linked
list. Then, we check if the linked list is not empty then we update the previous pointer of
the current head to the new node. Finally, we return the new node as the head of the
linked list.
#include <stdio.h>
struct Node {
int data;
struct Node *next;
struct Node *prev
};
// Return the new node as the head of the doubly linked list
return new_node;
}
int main() {
return 0;
}
Output
Original Linked List: 2 3 4
After inserting Node at the front: 1 2 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Given a Doubly Linked List, the task is to insert a new node at the end of the
linked list.
Examples:
Input: Linked List = 1 <-> 2 <-> 3, NewNode = 4
Output: Linked List = 1 <-> 2 <-> 3 <-> 4
Input: Linked List = NULL, NewNode = 1
Output: Linked List = 1
Approach:
Inserting at the end involves traversing the entire list until we reach the last
node. We then set the last node’s next reference to point to the new node
and new node's previous reference to point to the last node. Thus, making
the new node the last element in the list.
#include <stdio.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Function to insert a new node at the end of the doubly linked list
struct Node* insertEnd(struct Node *head, int new_data) {
struct Node *new_node = createNode(new_data);
// If the linked list is empty, set the new node as the head
if (head == NULL) {
head = new_node;
} else {
struct Node *curr = head;
while (curr->next != NULL) {
curr = curr->next;
}
return head;
}
return 0;
}
Output
Original Linked List: 1 2 3
Inserting Node with data 4 at the end: 1 2 3 4
Time Complexity: O(N), where N is the number of nodes in the linked list.
Auxiliary Space: O(1)
Given a doubly linked list, the task is to delete the last node of the given linked
list.
Examples:
Input: 1 <-> 2 <-> 3 <-> NULL
Output: 1 <-> 2 <-> NULL
Explanation: The last node of the linked list is 3, so 3 is deleted.
Input: 15 -> NULL
Output: NULL
Explanation: The last node of the linked list is 15, so 15 is deleted.
Approach:
To perform the deletion operation at the end of doubly linked list, we need to
traverse the list to find the second last node, then set its next pointer to null.
To delete a node at the end in doubly linked list, we can use the following steps:
Check if the doubly linked list is empty. If it is empty, then there is nothing to
delete.
If the list is not empty, then move to the last node of the doubly linked list,
say curr.
Update the second-to-last node’s next pointer to NULL, curr->prev->next =
NULL.
Free the memory allocated for the node that was deleted.
#include <stdio.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Corner cases
if (head == NULL)
return NULL;
if (head->next == NULL) {
free(head);
return NULL;
}
int main() {
printList(head);
return 0;
}
Output
Original Linked List: 1 2 3
After Deletion at the end: 1 2
Time Complexity: O(N), where N is the number of nodes in the linked list
Auxiliary Space: O(1)