349. XOR Linked List — Insert and Traverse

EasyLinked ListLinked ListXORMemory-Efficient

An XOR Linked List is a memory-efficient doubly linked list where each node stores the XOR of the addresses of the previous and next nodes.

Given a sequence of integer values, simulate an XOR linked list by inserting each value at the end. Then traverse the list from head to tail and return all values.

Since actual memory addresses are not available in most high-level languages, simulate the XOR structure by storing XOR of indices and verifying forward traversal produces the original sequence.

Input: An array of integers to insert into the XOR linked list.

Output: Array of integers obtained by forward traversal of the XOR linked list.

Examples

Example 1
Input: [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: Insert 1,2,3,4,5. XOR LL simulated. Forward traversal returns [1,2,3,4,5].
Example 2
Input: [10,20,30]
Output: [10,20,30]
Explanation: Three nodes inserted. Traversal: 10->20->30.
Example 3
Input: [7]
Output: [7]
Explanation: Single node. XOR prev=0, next=0. Returns [7].

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →