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:

1. If the input linked list is empty, we return null.
2. If there is only one node in the list, we return null after deleting that node.

### Algorithm:

• 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
cout << head->data << " ";
}
}
// Check if the linked list is empty
// Create a temporary pointer to the current head
// Move the head to the next node
delete temp;
}
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
// Print the modified linked list
return 0;
}
``````

Output: 5 8 7

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;
}
}
// Check if the linked list is empty
return null;
// Move the head to the next node
// 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) {
}
}
// 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
// Print the modified linked list
}
}
``````

Output: 5 8 7

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
return None
# There’s no need to delete the earlier head since it gets automatically deleted.
# Function to print the linked list
# 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
# Print the modified linked list
``````

Output: 5 8 7

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;
}
}
return null;
}
// There’s no need to delete the earlier head since it gets automatically deleted.
}
// Function to print the linked list
}
}
// 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
// Print the modified linked list
}
// Call the main function
main();
``````

Output: 5 8 7