[LeetCode][JavaScript]Copy List with Random Pointer

來源:互聯網
上載者:User

標籤:

Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

https://leetcode.com/problems/copy-list-with-random-pointer/      第一把直接暴力兩輪遍曆。第一輪遍曆copy鏈表,用hash表記錄下各個節點,第二亂遍曆去賦值鏈表裡的random對象。然後稍稍改進了一下,一次遍曆裡把能的找到的random都賦上,如果還沒new出來,記到missed數組中,最後把missed的節點中的random再賦上值。 舊版:
 1 var copyRandomList = function(head) { 2     var dict = {}; 3     var originNode = head; 4     var newNode = new RandomListNode(0); 5     var originHead = head; 6     var newHead = newNode; 7     while(originNode !== null){ 8         var temp = new RandomListNode(originNode.label); 9            dict[originNode.label] = temp;10            newNode.next = temp;11 12         newNode = newNode.next;13         originNode = originNode.next;14     }15 16     originNode = originHead;17     newNode = newHead.next;18     while(originNode !== null){19         if(originNode.random !== null){20             newNode.random = dict[originNode.random.label];21         }22         newNode = newNode.next;23         originNode = originNode.next;24     }25     return newHead.next;    26 };

新版:

 1 /** 2  * Definition for singly-linked list with a random pointer. 3  * function RandomListNode(label) { 4  *     this.label = label; 5  *     this.next = this.random = null; 6  * } 7  */ 8  9 /**10  * @param {RandomListNode} head11  * @return {RandomListNode}12  */13 var copyRandomList = function(head) {14     var dict = {};15     var missed = [];16     var originNode = head;17     var newNode = new RandomListNode(0);18     var originHead = head;19     var newHead = newNode;20     while(originNode !== null){21         var temp = new RandomListNode(originNode.label);22            dict[originNode.label] = temp;23            newNode.next = temp;24            newNode = newNode.next;25 26            if(originNode.random !== null){27                if(dict[originNode.random.label]){28                    newNode.random = dict[originNode.random.label];29                }else{30                    newNode.random = originNode.random.label;31                    missed.push(newNode.label);32                }33         }34         originNode = originNode.next;35     }36     for(var i = 0; i < missed.length; i++){37         var curr = dict[missed[i]];38         curr.random = dict[curr.random];39     }40     return newHead.next;    41 };

 

  

[LeetCode][JavaScript]Copy List with Random Pointer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.