Rotate image (square matrix) by 90 deg
Tue, 06 May 2025


We have only one pointer and that pointer will act as both the forward and the backward pointer. This is where XOR comes into play:
The basic operations on XOR (as also used in this post), has some interesting facts like,
if A ^ B = C Then C ^ A = B and C ^ B = A
i.e If we have C & A, then we can compute B and if we have C & B, then we can compute A.
We will use this property of XOR operator.
For each Node we will not store either the previous or next pointer, but we will store the XOR of previous & next pointer.
So if we have the below linked list

and if a1, a2, a3, a4 are the addresses of each node respectively, then the XOR list will store

If you are traversing in the forward direction then, you will get the address of next node as below:
| Current Node | Next Node | Value of Pointer =(Add of Prev Node) ^ (Link of Current Node) | Address of Next Node |
| list | a1 | Directly | - |
| a1 | a2 | NULL ^ Link of a2 | NULL^NULL^a2 = a2 |
| a2 | a3 | a1 ^ Link of a2 | a1^a1^a3 = a3 |
| a3 | a4 | a2 ^ Link of a3 | a2^a2^a4 = a4 |
| a4 | NULL | a3 ^ Link of a4 | a3^a3^NULL = NULL |
Hence at each point, you have to store the address of previous node to compute the address of next node.
Address of Next Node = Address of Previous Node ^ pointer in the current Node.
Similarly to compute the address of previous node you need to know the address of next node.
Address of Previous Node = Address of Next Node ^ pointer in the current Node
Advantage:
http://www.linuxjournal.com/article/6828?page=0,0 The C++ implementation of this is available here…
Tue, 06 May 2025
Tue, 06 May 2025
Tue, 06 May 2025
Leave a comment