zoukankan      html  css  js  c++  java
  • Copying Linked Lists with Random Pointers

    Copying Linked Lists with Random Pointers 

    Linked list with random pointers to other nodes in the list.

    两个方法:
    方法一:

    1.不考虑随机指针的情况下复制链表;

    2.在复制过程中建立一个以原链表节点地址为key,相应的复制链表节点地址为value的hash;

    3.遍历原链表和新链表,得到原链表随机指针值,并复制给新链表。

    方法二:

    1.忽略随机指针值复制链表第n个节点并插入到第n个节点与第n+1个节点之间,以此为方式修改链表直到链表尾。

    2.利用链表节点next指针指向节点的的拷贝这一已知,使用如下语句:

    1 srcCurrent->next->random = srcCurrent->random->next;

    Original linked list, but with the copy nodes inserted and random pointers assigned.

    3.分开链表

    srcCurrent->next = srcCurrent->next->next;
    cpyCurrent->next = cpyCurrent->next->next;
    

      

  • 相关阅读:
    JPA01
    mybatis入门
    PHP 循环- While循环
    PHP超级全局变量
    PHP 数组排序
    PHP数组
    PHP Switch语句
    PHP IF...Else语句
    PHP运算符
    PHP字符串变量
  • 原文地址:https://www.cnblogs.com/muzinian/p/3324992.html
Copyright © 2011-2022 走看看