XOR Linked List: Remove last node of the Linked List
Explanation: Deleting a node from the end modifies the given XOR Linked List to 4<–>7<–>9
Output: List is empty
Explanation: After deleting the only node present in the XOR Linked List, the list becomes empty.
Approach: The idea to solve this problem is to traverse the XOR linked list until the last node is reached and update the address of its previous node. Follow the steps below to solve the problem:
- If the XOR linked list is empty, then print “List is empty“.
- Traverse the XOR Linked List until the last node of the Linked List is reached.
- Update the address of its previous node.
- Delete the last node from memory.
- If the list becomes empty after deleting the last node, then print “List is empty”. Otherwise, print the remaining nodes of the linked list.
Below is the implementation of the above approach:
40 30 20
Time Complexity: O(N)
Auxiliary Space: O(1)