LeetCode: Copy List With Random Pointer Solution
1/**2 * // Definition for a Node.3 * function Node(val, next, random) {4 * this.val = val;5 * this.next = next;6 * this.random = random;7 * };8 */910/**11 * @param {Node} head12 * @return {Node}13 */14var copyRandomList = function (head) {15 let newHead16 let iter17 let prevIter1819 iter = head20 while (iter) {21 const node = new Node(iter.val)2223 if (prevIter) {24 prevIter.next = node25 } else {26 newHead = node27 }2829 prevIter = node30 iter.newRef = node31 iter = iter.next32 }3334 iter = head35 while (iter) {36 iter.newRef.random = iter.random?.newRef37 iter = iter.next38 }3940 return newHead41}
Comments
Loading comments...
Tags
leetcode
linked list
hash table
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
CodeWars: How Many Are Smaller Than Me
Feb 10, 2021