#include <bits/stdc++.h>
#include <inttypes.h>
using
namespace
std;
struct
Node {
int
data;
struct
Node* nxp;
};
struct
Node* XOR(
struct
Node* a,
struct
Node* b)
{
return
(
struct
Node*)((
uintptr_t
)(a) ^ (
uintptr_t
)(b));
}
struct
Node* insert(
struct
Node** head,
int
value)
{
if
(*head == NULL) {
struct
Node* node =
new
Node;
node->data = value;
node->nxp = XOR(NULL, NULL);
*head = node;
}
else
{
struct
Node* curr = *head;
struct
Node* prev = NULL;
struct
Node* node
=
new
Node();
curr->nxp = XOR(node, XOR(NULL, curr->nxp));
node->nxp = XOR(NULL, curr);
*head = node;
node->data = value;
}
return
*head;
}
void
printList(
struct
Node** head)
{
struct
Node* curr = *head;
struct
Node* prev = NULL;
struct
Node* next;
while
(curr != NULL) {
cout << curr->data <<
" "
;
next = XOR(prev, curr->nxp);
prev = curr;
curr = next;
}
}
struct
Node* NthNode(
struct
Node** head,
int
N)
{
int
count = 0;
struct
Node* curr = *head;
struct
Node* curr1 = *head;
struct
Node* prev = NULL;
struct
Node* prev1 = NULL;
struct
Node* next;
struct
Node* next1;
while
(count < N && curr != NULL) {
next = XOR(prev, curr->nxp);
prev = curr;
curr = next;
count++;
}
if
(curr == NULL && count < N) {
cout <<
"Wrong Input\n"
;
return
(
uintptr_t
)0;
}
else
{
while
(curr != NULL) {
next = XOR(prev, curr->nxp);
next1 = XOR(prev1, curr1->nxp);
prev = curr;
prev1 = curr1;
curr = next;
curr1 = next1;
}
cout << curr1->data <<
" "
;
}
}
int
main()
{
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);
NthNode(&head, 3);
return
(0);
}