复杂链表复制

来源:互联网 发布:淘宝卖家服务态度评分 编辑:程序博客网 时间:2024/05/17 02:14

题目

复杂链表的复制。一个链表的每个节点,有一个指向next指针指向下一个节点,还有一个random指针指向这个链表中的一个随机节点或者NULL,现在要求实现复制这个链表,返回复制后的新链表。

解法一

(暴力求解)
1. 这个复杂链表是以单链表为基础的,可以先复制一个单链表,random 指针在原链表上的位置与新链表是相对应的,建立同步投影指针 shadow,与 cur 在原链表上同步,找新链表上的 random ,(如果找不到,说明原链表指针指向了未知地址)。
2. 考虑复杂情况,原链表带环,可以找到入口点并记录,将带环链表转化为无环链表处理,再处理无环的情况。

写代码

  1. 处理空链表情况
  2. 用找环入口点函数判断是否带环
  3. 若带环,先复制尾,到入口点,复制环,又到入口点,复制完毕,拆环(原链表),保留尾,接环用。然后就是不带环链表 random 指针的复制。
  4. 若不带环,不带环链表复制。
  5. 复制 random 封装函数(要用两次,还很长)。

代码

//尾插void PushBackCom(ComplexNode** ppList, int x){    assert(ppList);    ComplexNode* newTail = (ComplexNode*)malloc(sizeof(ComplexNode));    newTail->data = x;    newTail->next = NULL;    newTail->random = NULL;    if (*ppList == NULL)        *ppList = newTail;    else{        ComplexNode* plist = *ppList;        while (plist->next != NULL){            plist = plist->next;        }        plist->next = newTail;    }}//求入口点ComplexNode* IntranceOfRingCom(ComplexNode* pList){    ComplexNode* fast = pList;    ComplexNode* slow = pList;    while (fast && fast->next)    {        fast = fast->next->next;        slow = slow->next;        if (fast == slow)            break;    }    if (fast != slow || fast->next == NULL)        return NULL;    while (pList != slow)    {        pList = pList->next;        slow = slow->next;    }    return pList;}void copyRandom(ComplexNode* cur, ComplexNode* n_cur){    ComplexNode* newComList = n_cur;    ComplexNode* pList = cur;    while (n_cur != NULL)    {        if (cur->random != NULL)        {            ComplexNode* shadow = newComList;            ComplexNode* cur2 = pList;            while (cur->random != cur2 && cur2 != NULL)            {                shadow = shadow->next;                cur2 = cur2->next;            }            n_cur->random = shadow;            if (shadow == NULL)//在这里出现NULL,意味着有random指向非本链表的位置                printf("warn : point to unknowed node.");        }        else            n_cur->random = NULL;        n_cur = n_cur->next;        cur = cur->next;    } }ComplexNode* copyComplexList(ComplexNode* pList){    if (pList == NULL)return NULL;    ComplexNode* intr = IntranceOfRingCom(pList);    ComplexNode* newComList = NULL;    PushBackCom(&newComList, pList->data);    if (intr != NULL)    {        ComplexNode* n_intr = newComList;         ComplexNode* n_cur = newComList;        ComplexNode* cur = pList;        while (cur != intr)//尾复制        {            cur = cur->next;            PushBackCom(&n_cur, cur->data);            n_cur = n_cur->next;            n_intr = n_intr->next;        }        while (cur->next != intr)//环复制        {            cur = cur->next;            PushBackCom(&n_cur, cur->data);            n_cur = n_cur->next;        }        copyRandom(pList, newComList);        //接环,现在n_cur刚好是尾        n_cur->next = n_intr;    }    else    {        ComplexNode* n_cur = newComList;        ComplexNode* cur = pList;        while (cur->next != NULL)        {            cur = cur->next;            PushBackCom(&n_cur, cur->data);            n_cur = n_cur->next;        }        copyRandom(pList, newComList);    }    return newComList;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109

方法二

这里写图片描述
(没有处理带环情况,可参考方法一处理带环情况)

代码

ComplexNode* copyComplexList2(ComplexNode* pList){    if (pList == NULL)return NULL;    ComplexNode* cur = pList;    ComplexNode* newcode;    while (cur != NULL)    {        newcode = (ComplexNode*)malloc(sizeof(ComplexNode));        newcode->data = cur->data;        newcode->next = cur->next;        newcode->random = NULL;        cur->next = newcode;        cur = cur->next->next;    }    cur = pList;    newcode = pList->next;    while (cur != NULL)    {        if (cur->random != NULL)            newcode->random = cur->random->next;        newcode = cur->next;        cur = cur->next->next;    }    newcode = pList->next;    ComplexNode* ret = pList->next;    cur = pList;    cur->next = cur->next->next;    cur = cur->next;    while (cur != NULL)    {        newcode->next = newcode->next->next;        cur->next = cur->next->next;        cur = cur->next;        newcode = newcode->next;    }    return ret;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
(function () {('pre.prettyprint code').each(function () { var lines = (this).text().split(\n).length;varnumbering = $('
    ').addClass('pre-numbering').hide(); (this).addClass(hasnumbering).parent().append(numbering); for (i = 1; i
    原创粉丝点击