给定一个XOR链表,任务是反转XOR链表。
例子:
Input: 4 <–> 7 <–> 9 <–> 7
Output: 7 <–> 9 <–> 7 <–> 4
Explanation:
Reversing the linked list modifies the XOR linked list to 7 <–> 9 <–> 7 <–> 4.
Input: 2 <-> 5 <-> 7 <-> 4
Output: 4 <-> 7 <-> 5 <-> 2
Explanation:
Reversing the linked list modifies the XOR linked list to 4 <-> 7 <-> 5 <-> 2.
方法: XOR链表由单个指针组成,这是双向遍历XOR链表所需的唯一指针。因此,解决此问题的想法仅是将XOR链表的最后一个节点设为Head Node 。请按照以下步骤解决问题:
- 初始化一个指针变量,例如curr ,以指向要遍历的当前节点。
- 将当前的头指针存储在curr变量中。
- 如果curr等于NULL ,则返回NULL 。
- 否则,遍历最后一个节点并使其成为XOR链接列表的头部。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// 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
Node* nxp;
};
// Function to find the XOR of two nodes
Node* XOR(Node* a, Node* b)
{
return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
// Function to insert a node with
// given value at begining position
Node* insert(Node** head, int value)
{
// If XOR linked list is empty
if (*head == NULL)
{
// Initialize a new Node
Node* node
= new 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
Node* curr = *head;
// Stores the address
// of previous node
Node* prev = NULL;
// Initialize a new Node
Node* node
= new 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(Node** head)
{
// Stores XOR pointer
// in current node
Node* curr = *head;
// Stores XOR pointer of
// in previous Node
Node* prev = NULL;
// Stores XOR pointer of
// in next node
Node* next;
// Traverse XOR linked list
while (curr != NULL)
{
// Print current node
cout<data<<" ";
// Forward traversal
next = XOR(prev, curr->nxp);
// Update prev
prev = curr;
// Update curr
curr = next;
}
cout << endl;
}
// Function to reverse the XOR linked list
Node* reverse(Node** head)
{
// Stores XOR pointer
// in current node
Node* curr = *head;
if (curr == NULL)
return NULL;
else
{
// Stores XOR pointer of
// in previous Node
Node* prev = NULL;
// Stores XOR pointer of
// in next node
Node* next;
while (XOR(prev, curr->nxp) != NULL)
{
// Forward traversal
next = XOR(prev, curr->nxp);
// Update prev
prev = curr;
// Update curr
curr = next;
}
// Update the head pointer
*head = curr;
return *head;
}
}
// Driver Code
int main()
{
/* Create following XOR Linked List
head-->40<-->30<-->20<-->10 */
Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
/* Reverse the XOR Linked List to give
head-->10<-->20<-->30<-->40 */
cout << "XOR linked list: ";
printList(&head);
reverse(&head);
cout << "Reversed XOR linked list: ";
printList(&head);
return 0;
}
// This code is contributed by rutvik_56.
C
// C program for the above approach
#include
#include
#include
// 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 begining 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;
}
printf("\n");
}
// Function to reverse the XOR linked list
struct Node* reverse(struct Node** head)
{
// Stores XOR pointer
// in current node
struct Node* curr = *head;
if (curr == NULL)
return NULL;
else {
// Stores XOR pointer of
// in previous Node
struct Node* prev = NULL;
// Stores XOR pointer of
// in next node
struct Node* next;
while (XOR(prev, curr->nxp) != NULL) {
// Forward traversal
next = XOR(prev, curr->nxp);
// Update prev
prev = curr;
// Update curr
curr = next;
}
// Update the head pointer
*head = curr;
return *head;
}
}
// Driver Code
int main()
{
/* Create following XOR Linked List
head-->40<-->30<-->20<-->10 */
struct Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
/* Reverse the XOR Linked List to give
head-->10<-->20<-->30<-->40 */
printf("XOR linked list: ");
printList(&head);
reverse(&head);
printf("Reversed XOR linked list: ");
printList(&head);
return (0);
}
输出:
XOR linked list: 40 30 20 10
Reversed XOR linked list: 10 20 30 40
时间复杂度: O(N)
辅助空间: O(1)