[LeetCode] 138. Copy List with Random Pointer java__java

來源:互聯網
上載者:User
    /**138. Copy List with Random Pointer     * @param head     * @return 深度複製一個鏈表,使用重新分配記憶體儲存新的     */    public RandomListNode copyRandomList(RandomListNode head) {        if (head == null || head.next == null) {            return head;        }        Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();        RandomListNode ret = new RandomListNode(head.label);        map.put(head, ret);        RandomListNode rhead = ret;        RandomListNode phead = head;        while (head != null) {            RandomListNode node = new RandomListNode(head.label);            map.put(head, node);            ret.next = node;            ret = ret.next;            head = head.next;        }        ret = rhead;        head = phead;        while(head != null) {            ret.random = map.get(head.random);            ret = ret.next;            head = head.next;        }        return rhead;      }
/*HashMap的key存原始pointer,value存新的pointer。第一遍,先不copy random的值,只copy數值建立好新的鏈表。並把新舊pointer存在HashMap中。第二遍,遍曆舊錶,複製random的值,因為第一遍已經把鏈表複製好了並且也存在HashMap裡了,所以只需從HashMap中,把當前舊的node.random作為key值,得到新的value的值,並把其賦給新node.random就好。 * */
    public RandomListNode copyRandomList1(RandomListNode head) {          if (head == null)              return head;          /*第一步:在OldList的每個節點後面都插入一個copyNode(拷貝鏈表的結點)*/          RandomListNode nowNode = head;          while (nowNode != null){              RandomListNode copyNode = new RandomListNode(nowNode.label);              copyNode.random = nowNode.random;              copyNode.next = nowNode.next;              nowNode.next = copyNode;              nowNode = nowNode.next.next;          }          /*第二步:確定NewList的每個節點,真正關聯到的Random結點是哪個,          *      因為第一步已經把所有NewList上的結點都建立了*/          nowNode = head;          while (nowNode != null){              if (nowNode.random != null){                  nowNode.next.random = nowNode.random.next;              }              nowNode = nowNode.next.next;          }          /*第三步:還原OldList的next為一開始的next結點          *      並拼接NewList的next到它真正所應該關聯的next結點          *      即:保持老鏈表OldList不變,拼接新鏈表NewList。          * */          RandomListNode pHead = new RandomListNode(0);          pHead.next = head;          RandomListNode newlist = pHead;          nowNode = head;          while (nowNode != null){              pHead.next = nowNode.next;              nowNode.next = pHead.next.next;              pHead = pHead.next;              nowNode = nowNode.next;          }          return newlist.next;      } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.