To perform the deletion operation at the end of linked list, we need to traverse the list to find the second last node, then set its next pointer to null. If the list is empty then there is no node to delete or has only one node then point head to null.
Step-by-step approach:
Check if list is empty then return NULL.
If the list has only one node then delete it and return NULL.
Traverse the list to find the second last node. => Set the next pointer of second last node to NULL.
Implementation:
Output
1 -> 2 -> 3 -> 4 -> nullptr
Time Complexity: O(n), traversal of the linked list till its end, so the time complexity required is O(n). Auxiliary Space: O(1)