-
Notifications
You must be signed in to change notification settings - Fork 4
added copyListWithRandomPointer #6
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
base: main
Are you sure you want to change the base?
Conversation
| @@ -0,0 +1,60 @@ | |||
| const copyRandomList = (head) => { | |||
| let copies = new Map(); | |||
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
e286181 to
d69c4be
Compare
No description provided.