Skip to content

feat: add javascript solution to lcof problem: No.35 #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lcof/面试题35. 复杂链表的复制/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,52 @@ public class Solution {

### **JavaScript**

- 哈希表

```js
/**
* // Definition for a Node.
* function Node(val, next, random) {
* this.val = val;
* this.next = next;
* this.random = random;
* };
*/

/**
* @param {Node} head
* @return {Node}
*/
var copyRandomList = function (head) {
if (head == null) {
return head
}

let cur = head
const map = new Map();
while (cur != null) {
map.set(cur, new Node(cur.val))
cur = cur.next
}

const res = new Node();
let newCur = res
cur = head
while (cur != null) {
const node = map.get(cur)
node.random = map.get(cur.random)
newCur.next = node
newCur = node
cur = cur.next
}

return res.next
};

```

- 原地算法

```js
/**
* // Definition for a Node.
Expand Down