Skip to content

Conversation

@rohinmanvi
Copy link
Contributor

No description provided.

@rohinmanvi rohinmanvi changed the title added added copyListWithRandomPointer Jun 6, 2022
@@ -0,0 +1,60 @@
const copyRandomList = (head) => {
let copies = new Map();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining why you used a Map instead of an object (this is an object const x = {};).

I believe the reason is because you want to use an instance of the Node class as your key (JS objects only allow strings as the keys).

let copy = new Node(current.val, current.next, null);
copy.next = current.next;
current.next = copy;
current = current.next;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do current = current.next.next;, no need for this to be 2 lines.

};

/*
O(1) space solution. Honestly, don't even worry about knowing how to do this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this seems like something you shouldn't have to know how to do. But, on my Amazon interviews, I got this question and the interviewer definitely seemed to expect me to get the O(1) solution (I finished the O(n) one pretty fast and she spent quite a long time watching me struggled to get O(1)). I think we should still tell them to not worry about knowing how to do this, but what do you think?

O(1) space solution. Honestly, don't even worry about knowing how to do this.
*/
const copyRandomList = (head) => {
if (!head) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment or some comments explaining how this works, I think it's not very intuitive.

const copyRandomList = (head) => {
if (!head) return null;

let current = head;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every variable in this function can be turned into a const instead of let. const means that you cannot reassign variables e.g.
const x = {};
x = {h: 5};

That is bad^

But this is fine:
const x = {};
x.h = 5;

Change this in the O(n) solution too

}

current = head;
let copy = head.next;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name this better. Maybe like headOfCopy.

@Shreshth3 Shreshth3 force-pushed the main branch 2 times, most recently from e286181 to d69c4be Compare June 9, 2022 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants