Problem Statement: Given a linked list, delete the head of the linked list and print the linked list with the updated head.
Examples
Example 1:
Examples:
Input Format: 0->1->2
Result: 1->2
Explanation: The head of the list is the first node. After removing the head and shifting the head pointer to the next node gives this result.
Example 2:
Input Format: 12->5->8->7
Result: 5->8->7
Explanation: Again, after deleting the head and updating the linked list, the list starts from the next node, which is the new head.
Solution
Disclaimer: Don’t jump directly to the solution, try it out yourself first.
Approach:
To get the new linked list, the main process includes updating the node after the head as the new head and clearing up the memory occupied by the original head (only in the case of C++), effectively deleting the head of the linked list.
Two edge cases to consider are:
If the input linked list is empty, we return null.
If there is only one node in the list, we return null after deleting that node.
Algorithm:
Initialize a pointer temp pointing to the head of the linked list. In order to make the second node the new head of the linked, replace the head with the next node of the head in the linked list [head=head->next].
Doing this will leave behind the first node of the linked list with the pointer pointing to, delete this node to free up the occupied space. Note: In the case of languages like Java, Python, and Javascript, there is no need for the deletion of objects or nodes because these have an automatic garbage collection mechanism that automatically identifies and reclaims memory that is no longer in use.
At the end, return the new head of the linked list.
Code:
C++ Code
// Node class definition
class Node {
public:
int data;
Node* next;
// Constructor with both data and next pointer
Node(int data1, Node* next1) {
data = data1;
next = next1;
}
// Constructor with only data (next pointer set to nullptr)
Node(int data1) {
data = data1;
next = nullptr;
}
};
// Function to print the linked list
void printLL(Node* head) {
while (head != NULL) {
cout << head->data << " ";
head = head->next;
}
}
// Function to remove the head of the linked list
Node* removesHead(Node* head) {
// Check if the linked list is empty
if (head == NULL)
return head;
// Create a temporary pointer to the current head
Node* temp = head;
// Move the head to the next node
head = head->next;
// Delete the original head
delete temp;
// Return the updated head of the linked list
return head;
}
int main() {
// Initialize a vector with integer values
vector<int> arr = {12, 5, 8, 7};
// Create the linked list with nodes initialized with vector values
Node* head = new Node(arr[0]);
head->next = new Node(arr[1]);
head->next->next = new Node(arr[2]);
head->next->next->next = new Node(arr[3]);
// Remove the head of the linked list
head = removesHead(head);
// Print the modified linked list
printLL(head);
return 0;
}
Output: 5 8 7
Time Complexity: O(1) for updating the head of the linked list.
Space Complexity: O(1), as we have not used any extra space.
Java Code
// Node class definition
class Node {
int data;
Node next;
// Constructor with both data and next pointer
Node(int data1, Node next1) {
this.data = data1;
this.next = next1;
}
// Constructor with only data (next pointer set to null)
Node(int data1) {
this.data = data1;
this.next = null;
}
}
// LinkedList class
public class LinkedList {
// Method to remove the head of the linked list
private static Node removesHead(Node head) {
// Check if the linked list is empty
if (head == null)
return null;
// Move the head to the next node
head = head.next;
// Return the updated head of the linked list
return head;
// There’s no need to delete the earlier head since it gets automatically deleted.
}
// Method to print the linked list
private static void printLL(Node head) {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
// Main method
public static void main(String[] args) {
// Initialize an array with integer values
int[] arr = { 2, 5, 8, 7 };
// Create the linked list with nodes initialized with array values
Node head = new Node(arr[0]);
head.next = new Node(arr[1]);
head.next.next = new Node(arr[2]);
head.next.next.next = new Node(arr[3]);
// Remove the head of the linked list
head = removesHead(head);
// Print the modified linked list
printLL(head);
}
}
Output: 5 8 7
Time Complexity: O(1) for updating the head of the linked list.
Space Complexity: O(1), as we have not used any extra space.
Python Code
class Node:
def __init__(self, data1, next1=None):
self.data = data1
self.next = next1
# Function to remove the head of the linked list
def removes_head(head):
if head is None:
return None
head = head.next
return head
# There’s no need to delete the earlier head since it gets automatically deleted.
# Function to print the linked list
def print_ll(head):
while head is not None:
print(head.data, end=" ")
head = head.next
# Main function
if __name__ == "__main__":
# Initialize an array with integer values
arr = [2, 5, 8, 7]
# Create the linked list with nodes initialized with array values
head = Node(arr[0])
head.next = Node(arr[1])
head.next.next = Node(arr[2])
head.next.next.next = Node(arr[3])
# Remove the head of the linked list
head = removes_head(head)
# Print the modified linked list
print_ll(head)
Output: 5 8 7
Time Complexity: O(1) for updating the head of the linked list.
Space Complexity: O(1), as we have not used any extra space.
JavaScript Code
// Node class definition
class Node {
constructor(data1, next1 = null) {
this.data = data1;
this.next = next1;
}
}
// Function to remove the head of the linked list
function removesHead(head) {
if (head === null) {
return null;
}
head = head.next;
return head;
// There’s no need to delete the earlier head since it gets automatically deleted.
}
// Function to print the linked list
function printLL(head) {
while (head !== null) {
console.log(head.data + " ");
head = head.next;
}
}
// Main function
function main() {
// Initialize an array with integer values
const arr = [2, 5, 8, 7];
// Create the linked list with nodes initialized with array values
let head = new Node(arr[0]);
head.next = new Node(arr[1]);
head.next.next = new Node(arr[2]);
head.next.next.next = new Node(arr[3]);
// Remove the head of the linked list
head = removesHead(head);
// Print the modified linked list
printLL(head);
}
// Call the main function
main();
Output: 5 8 7
Time Complexity: O(1) for updating the head of the linked list.
Space Complexity: O(1), as we have not used any extra space.
In case you are learning DSA, you should definitely check out our free A2Z DSA Course with videos and blogs.
Special thanks to Neerav Sethi for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article