Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Clone Random Pointer Linked List (Main Thread)

Here is the interview question prompt, presented for reference.

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.

![image](https://storage.googleapis.com/algodailyrandomassets/curriculum/linked-lists/clone-random-pointer-linked-list/problem.png)

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.

You can see the full challenge with visuals at this link.

Challenges • Asked almost 2 years ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Dec 17, 2022:

This is the main discussion thread generated for Clone Random Pointer Linked List (Main Thread).