Timer is Running

XOR Linked List
Submissions: 2590   Accuracy:


  Difficulty: Easy   Marks: 2

An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory efficient version of Doubly Linked List can be created using only one space for address field with every node. This memory efficient Doubly Linked List is called XOR Linked List or Memory Efficient as the list uses bit-wise XOR operation to save space for one address.
Given stream of data of size N for the linked list, your task is to complete the function insert() and printList(). The insert() function pushes (or inserts at beginning) the given data in the linked list and the printList() function prints the linked list first in forward direction and then in backward direction.
Note: There is an utility function XOR() that takes two Node pointer to get the bit-wise XOR of the two Node pointer. Use this function to get the XOR of the two pointers.

The insert function takes 2 arguments as input, the head of the linked list and an integer data to be inserted in the linked list. This function should return the head after inserting new node.
The printList function takes a single argument as input, which is a pointer pointing to the head of the linked list.
There are multiple test cases and for each test the function will be called separately.

For each test function printList first should print the linked list in the forward direction and then in the backward direction.

User Task:
The task is to complete the function insert() and printList() as required.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

1 <= T <= 100
1 <= N <= 100


9 5 4 7 3 10
58 96 31

10 3 7 4 5 9
9 5 4 7 3 10
31 96 58
58 96 31


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.