📜  XOR链表–查找中间节点

📅  最后修改于: 2021-05-25 10:03:50             🧑  作者: Mango

给定一个XOR链表,任务是找到给定XOR链表的中间节点

例子:

方法:请按照以下步骤解决问题:

  • 遍历到“链表”的(Length / 2)个节点。
  • 如果发现节点数为奇数,则将(Length +1)/ 2th节点打印为唯一的中间节点。
  • 如果发现节点数为偶数,则同时打印“ Length / 2th”节点和(“ Length / 2” +1)节点作为中间节点。

下面是上述方法的实现:

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 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 the middle node
int printMiddle(struct Node** head, int len)
{
    int count = 0;
  
    // 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;
  
    int middle = (int)len / 2;
  
    // Traverse XOR linked list
    while (count != middle) {
  
        // Forward traversal
        next = XOR(prev, curr->nxp);
  
        // Update prev
        prev = curr;
  
        // Update curr
        curr = next;
  
        count++;
    }
  
    // If the length of the
    // linked list is odd
    if (len & 1) {
        printf("%d", curr->data);
    }
  
    // If the length of the
    // linked list is even
    else {
        printf("%d %d", prev->data,
               curr->data);
    }
}
  
// Driver Code
int main()
{
    /* Create following XOR Linked List
    head --> 4 –> 7 –> 5 */
    struct Node* head = NULL;
    insert(&head, 4);
    insert(&head, 7);
    insert(&head, 5);
  
    printMiddle(&head, 3);
  
    return (0);
  
}


输出:
7

时间复杂度: O(N)
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。