I found this code online for reversing a linked list using only 2 pointers using xor operation :
void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
// at last prev points to new head
while (current != NULL) {
current = (struct Node*)((ut)prev ^ (ut)current ^ (ut)(current->next) ^ (ut)(current->next = prev) ^ (ut)(prev = current));
}
*head_ref = prev;
}
Can you please explain how this code works ?