AlgoDaily Solution

1var assert = require('assert');
2
3function Node(val, next, random) {
4    this.val = val;
5    this.next = next;
6    this.random = random;
7};
8
9function cloneRandomList(head) {
10    if (head === null) {
11        return null;
12    }
13
14    let currentNode = head;
15    while (currentNode) {
16        let temp = new Node(currentNode.val);
17        temp.next = currentNode.next;
18        currentNode.next = temp;
19        currentNode = currentNode.next.next;
20    }
21
22    currentNode = head;
23    while (currentNode) {
24        if (currentNode.random) {
25            currentNode.next.random = currentNode.random.next;
26        } else {
27            currentNode.next.random = null;
28        }
29        currentNode = currentNode.next.next;
30    }
31
32    currentNode = head;
33    let copyHead = head.next;
34    while (currentNode.next) {
35        temp = currentNode.next;
36        currentNode.next = currentNode.next.next;
37        currentNode = temp;
38    }
39    return copyHead;
40}
41
42
43try {
44    let head = new Node(1);
45    head.next = new Node(2);
46    head.random = head.next;
47    head.next.next = null;
48    head.next.random = head.next;
49    let lst, random = listToString(head);
50    let lstAns, randomAns = listToString(cloneRandomList(head));
51    assert.equal(lst, lstAns);
52    assert.equal(random, randomAns);
53    console.log('PASSED: ' + "`cloneRandomList([[1, 1],[2, 1]]) should return `[[1, 1],[2, 1]]`");
54} catch (err) {
55    console.log(err);
56}
57
58try {
59    let head = new Node(3);
60    head.next = new Node(3);
61    head.random = null;
62    head.next.next = new Node(3);
63    head.next.random = head;
64    head.next.next.next = null;
65    head.next.next.random = null;
66    let lst, random = listToString(head);
67    let lstAns, randomAns = listToString(cloneRandomList(head));
68    assert.equal(lst, lstAns);
69    assert.equal(random, randomAns);
70    console.log('PASSED: ' + "`cloneRandomList([[3, null], [3, 0], [3, null]]) should return `[[3, null], [3, 0], [3, null]]`");
71} catch (err) {
72    console.log(err);
73}
74
75try {
76    let head = new Node(7);
77    head.next = new Node(13);
78    head.next.next = new Node(11);
79    head.next.next.next = new Node(10);
80    head.next.next.next.next = new Node(1);
81    head.random = null;
82    head.next.random = head;
83    head.next.next.random = head.next.next.next.next;
84    head.next.next.next.random = head.next.next;
85    head.next.next.next.next.random = head;
86    let lst, random = listToString(head);
87    let lstAns, randomAns = listToString(cloneRandomList(head));
88    assert.equal(lst, lstAns);
89    assert.equal(random, randomAns);
90    console.log('PASSED: ' + "`cloneRandomList([[7, null], [13, 0], [11, 4], [10, 2], [1, 0]]) should return `[[7, null], [13, 0], [11, 4], [10, 2], [1, 0]]`");
91} catch (err) {
92    console.log(err);
93}
94
95function listToString(head) {
96    var toPrint = [];
97    var toPrintRandom = [];
98    var currNode = head;
99    while (currNode) {
100        toPrint.push(currNode.val);
101        if (currNode.random != null) {
102            toPrintRandom.push(currNode.random.val);
103        }
104        currNode = currNode.next;
105    }
106    return toPrint.join(' -> '), toPrintRandom.join(' -> ');
107}

Community Solutions

Community solutions are only available for premium users.

Access all course materials today

The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.

Returning members can login to stop seeing this.

JAVASCRIPT
OUTPUT
Results will appear here.