给定一个XOR链表,任务是删除XOR链表的第一个节点。
例子:
Input: XLL = 4 < – > 7 < – > 9 < – > 7
Output: 7 < – > 9 < – > 7
Explanation:
Removing the first node of the XOR linked list modifies XLL to 7 < – > 9 < – > 7
Input: XLL = NULL
Output: List Is Empty
方法:想法是将XOR链表的头节点更新为XOR链表的第二个节点,并删除为第一个节点分配的内存。请按照以下步骤解决问题:
- 检查XOR链接列表是否为空。如果发现是真的,则打印“列表为空” 。
- 否则,将XOR链表的头节点更新为链表的第二个节点。
- 从内存中删除第一个节点。
下面是上述方法的实现:
C
// C program to implement
// 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 address 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)
{
// Check 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;
}
}
// Function to remove the first node form
// the given linked list
struct Node* delBeginning(struct Node** head)
{
// If list is empty
if (*head == NULL)
printf("List Is Empty");
else {
// Store the node to be deleted
struct Node* temp = *head;
// Update the head pointer
*head = XOR(NULL, temp->nxp);
// When the linked list
// contains only one node
if (*head != NULL) {
// Update head node address
(*head)->nxp
= XOR(NULL, XOR(temp,
(*head)->nxp));
}
free(temp);
}
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);
// Delete the first node
delBeginning(&head);
/* Print the following XOR Linked List
head-->30<-->20<-->10 */
printList(&head);
return (0);
}
输出:
30 20 10
时间复杂度: O(1)
辅助空间: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。