Write a program to delete last node from the given Linked List.
In order to delete last node, we need to set the second last node's next pointer to NULL value.
Our approach is:
- We should consider 3 cases here.
- When head node is null, return null.
- When the linked list contains only head node, then return null.
- In any other case, traverse the linked list and check node.next.next must not null in loop exit condition.
- Once after out of loop, set the node next reference to null and return the head node reference.
|