Given a XOR linked list and an integer K, the task is to reverse first K nodes in the given XOR linked list.
Examples :
Input : 7<–>6<–>8<–>11<–>3<–>1,K=3 Output : 8<–>6<–>7<–>11<–>3<–>1 Explanation : First 3 nodes are reverse to give a new Xor list Input : 7<–>6<–>8<–>11<–>3<–>1<–>2<–>0,K=9 Output : Wrong Input Explanation : Xor linked list is of size 8 and the given value of K is 9 which is greater than size.
Approach :
Follow the steps below to solve the problem:

- If head is equal to NULL then return NULL
- Else if Size of Xor linked list is less than K then return the head.
- Else traverse K nodes of the linked list from the given head.
- Disconnect Kth node from (K+1)th node.
- Connect the head pointer with (K+1)th node.
- Connect (K+1)th node with the head pointer if the (K+1)th node is not NULL.
- Make Kth node the new head.
Implementation :
// C program to implement
// the above approach
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
// Structure of a node
// in XOR linked list
struct Node {
// Stores data value
// of a node
int data;
// Stores XOR of previous
// pointer and next pointer
struct Node* nxp;
};
// Function to find the XOR of two nodes
struct Node* XOR(struct Node* a, struct Node* b)
{
return (struct Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
// Function to insert a node with
// given value at given position
struct Node* insert(struct Node** head, int value)
{
// If XOR linked list is empty
if (*head == NULL) {
// Initialize a new Node
struct Node* node
= (struct Node*)malloc(sizeof(struct Node));
// Stores data value in
// the node
node->data = value;
// Stores XOR of previous
// and next pointer
node->nxp = XOR(NULL, NULL);
// Update pointer of head node
*head = node;
}
// If the XOR linked list
// is not empty
else {
// Stores the address
// of current node
struct Node* curr = *head;
// Stores the address
// of previous node
struct Node* prev = NULL;
// Initialize a new Node
struct Node* node
= (struct Node*)malloc(sizeof(struct Node));
// Update curr node address
curr->nxp = XOR(node, XOR(NULL, curr->nxp));
// Update new node address
node->nxp = XOR(NULL, curr);
// Update head
*head = node;
// Update data value of
// current node
node->data = value;
}
return *head;
}
// Function to print elements of
// the XOR Linked List
void printList(struct Node** head)
{
// Stores XOR pointer
// in current node
struct Node* curr = *head;
// Stores XOR pointer of
// in previous Node
struct Node* prev = NULL;
// Stores XOR pointer of
// in next node
struct Node* next;
// Traverse XOR linked list
while (curr != NULL) {
// Print current node
printf("%d ", curr->data);
// Forward traversal
next = XOR(prev, curr->nxp);
// Update prev
prev = curr;
// Update curr
curr = next;
}
}
// Reverse the linked list in group of K
struct Node* reverseK(struct Node** head, int K, int len)
{
struct Node* curr = *head;
// If head is NULL
if (curr == NULL)
return NULL;
// If the Size of Xor linked list is less
// than K
else if (len < K)
return *head;
else {
int count = 0;
// Stores XOR pointer of
// in previous Node
struct Node* prev = NULL;
// Stores XOR pointer of
// in next node
struct Node* next;
while (count < K) {
// Forward traversal
next = XOR(prev, curr->nxp);
// Update prev
prev = curr;
// Update curr
curr = next;
// Count number of nodes processed
count++;
}
// Disconnect prev node from the next node
prev->nxp = XOR(NULL, XOR(prev->nxp, curr));
// Connect the head pointer with the prev
(*head)->nxp = XOR(XOR(NULL, (*head)->nxp), curr);
// Connect prev with the head
if (curr != NULL)
curr->nxp = XOR(XOR(curr->nxp, prev), *head);
return prev;
}
}
// Driver Code
int main()
{
/* Create following XOR Linked List
head-->7<–>6<–>8<–>11<–>3<–>1<–>2<–>0*/
struct Node* head = NULL;
insert(&head, 0);
insert(&head, 2);
insert(&head, 1);
insert(&head, 3);
insert(&head, 11);
insert(&head, 8);
insert(&head, 6);
insert(&head, 7);
head = reverseK(&head, 3, 8);
if (3 <= 8)
printList(&head);
else
printf("Wrong Input");
return (0);
}
Time Complexity : O(N)
Auxiliary Space : O(1)