Mark As Completed Discussion

Good afternoon! Here's our prompt for today.

You are given a head of a linked list of length n such that each node contains an additional random pointer. This random pointer may point to any other node in the list or is null.

Create a deep copy of the given linked list, and return its head such that no additional space is used. The deep copy should consist of exactly n brand new nodes. Each new node must have the following properties:

  • Its value is set to the value of its corresponding original node.
  • Both the next and random pointers of the new node should point to new nodes in the copied list, such that the pointers in the original list and copied list represent the same list state.
  • None of the pointers in the new list should point to nodes in the original list.

Question

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

Constraints

  • 0 <= n <= 1000
  • 104 <= Node.val <= 104
  • Node.random is null or points to some node in the linked list.

Try to solve this here or in Interactive Mode.

How do I practice this challenge?

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

Here's how we would solve this problem...

How do I use this guide?