力扣刷题:12-复制带随机指针的链表(138)

104 阅读1分钟

我是这样实现功能的:先复制一份random指针都为空的链表,然后计算原链表中每一个节点的random指针指向节点的索引位置,通过这个位置,在新链表中找到对应的新节点地址,并将它赋值给新链表对应位置节点的random指针变量。

下面是C语言实现的代码:

/**
 * Definition for a Node.
 * struct Node {
 *     int val;
 *     struct Node *next;
 *     struct Node *random;
 * };
 */

struct Node* newNode() {
    struct Node* ret = (struct Node*)malloc(sizeof(struct Node));
    ret->val = 0;
    ret->next = NULL;
    ret->random = NULL;
    return ret;
}

int countNodeIndex(struct Node* target, struct Node* head) {
    if (target == NULL) {
        return -1;
    }
    int index = -1;
    while (head) {
        index ++;
        if (target == head) {
            return index;
        }
        head = head->next;
    }
    return index;
}

int indexNode(int index, struct Node* head) {
    while (index-- && head) {
        head = head->next;
    }
    return head;
}

struct Node* copyRandomList(struct Node* head) {
	if (head == NULL) {
        return NULL;
    }
    struct Node* ret = newNode();
    struct Node* o = head; // 源链表指针
    struct Node* n = ret; // 新链表指针
    int count = 0;
    do {
        count ++;
        // 生成链表,维护好next指针,random留空
        n->next = newNode();
        n = n->next;
        n->val = o->val;
        o = o->next;
    } while (o);

    // 处理random指针
    o = head;
    n = ret->next;
    int randomTo;
    for (int i = 0; i < count; i++) {
        randomTo = countNodeIndex(o->random, head);
        n->random = indexNode(randomTo, ret->next);
        o = o->next;
        n = n->next;
    }

    return ret->next;
}