Pseudo code:
Given kth and k+1 node address in a single linked list you have to find out previous node consider you do not have address of head node.
Detail about implementation :
By first seeing it seems impossible task but it can be easily done using property of XOR gate.
Ex.
A xor A = 0
A xor NULL = A
C=B xor D ===> C xor D will give you B.
So the idea is Create a singly linked list where next pointer is made by doing Xor operation of previous node and next node.
Linked List : a -> b -> c -> d -> e -> f
a = null xor b
b = a xor c
c = b xor d
e = d xor f
f = e xor null
Steps :
1) If linked address of node b and c is given in that case we can easily find address of node a by applying xor operation.
Here b = (a xor c) so we can easily get 'a' by applying xor operation with c.
(a xor c) xor c ==> a.
No comments:
Post a Comment