How the Internet of Things is Changing the Way We Live

A network of connected machines, cars, and home appliances known as the Internet of Things (IoT) allows for data transmission and communication over the internet. The Internet of Things has been…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Deep Copy a Linked List

You are given a singly Linked List, return a deep copy of the list.

Singly Linked List with next pointers

Deep copy of a Linked List means we do not copy the references of the nodes of the original Linked List rather for each node in the original Linked List a new node is created.

Something like this…

We create an exact copy of the original list. Notice every node has a new copy.

Recursion is a very intuitive approach for copying a Linked List. Let’s understand how. Every recursive call is responsible for creating a new node corresponding to the original node. The next pointer of the newly created node is then made to point to the new node we get from the next recursion.

Let’s break it down further and look into the recursion calls. In the forward step we create all the new copied nodes first and then while backtracking we set all the next pointers.

As we move into the recursion we create copies of each node.
When the recursion reaches end and starts backtracking we return the newly created node reference. Thus, a reference to F’ is returned to its previous call. This continues until all the nodes are linked and we reach the head.

By the time the recursion ends we would have created all the new nodes and would have linked the nodes to each other.

There is also a memory efficient iterative solution for this problem. However, the aim of this article is not to focus on the various approaches for a single problem but rather to bring forth the differences in similar kinds of solutions for seemingly similar problems.

You are given a doubly Linked List, return a deep copy of the list.

This problem is very similar to the Problem 1, it’s just that we need to take care of the previous pointers too.

We can achieve this in the same recursive manner. Every forward step creates a new node and the next pointers get assigned while back tracking.

Wait ! What? 🤔 What about previous pointers!?

Let me rephrase it a bit.

Every forward step creates a new node, assigns the previous pointer and passes the new node to its next step and the next pointers get assigned while back tracking.

As we move into the recursion we create copies of each node and assign the prev pointer of each node to its previous node.
When the recursion reaches end and starts backtracking we return the newly created node reference. Thus, a reference to F’ is returned to its previous call. This continues until all the nodes are linked and we reach the head.

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

This problem is pretty interesting, even though we still have to copy a Linked List. What differentiates it from the other two problems is that this one involves a Linked List with random pointers.

This means the Linked List is still a doubly Linked List, but instead of the prev pointer we have a random pointer which can point to any node in the entire Linked List or can be null.

Since the pointers are random, the clichéd recursive approach won’t work as is. We can still do recursion but with some modifications.

Let’s first see how different are random pointers from next and prev pointers.

Since the random pointers do not just point to the previous nodes, we can’t pass previous nodes reference in recursion to assign it to the random pointer.

By following a random pointer we might encounter an unseen node in the Linked List. Of course, the intuition still is to clone any new node we come across. But since the nodes can be randomly accessed/traversed now, how do we figure out whether a node was already visited? Thus, this problem is more about how to save the reference of the nodes somewhere so that it can be reused by a random pointer or by either of the pointers.

Note, we need to save not just the reference of the old nodes but their mapping to the new nodes. So that if the pointers take us to the same node in the original list we know which new node we had created previously.

There are some exciting approaches we can take here -

For basic approaches you can refer:

For detailed analysis on all the approaches you can refer the official solution:

Share your views in the comment section. If you have any suggestions for other spot-the-difference problems please share.
Hit the clap 👏 button if you liked the article. 💛

Add a comment

Related posts:

Belize Tourism Board unveils redesigned TravelBelize.Org

The official destination marketing organization for Belize — the Belize Tourism Board — invites visitors to explore its newly redesigned TravelBelize website. The revamped webpage is the country’s…

Why Marketing Automation?

Now that we have seen what is marketing automation, what exactly should you automate when you do marketing automation, let’s have a quick look at how your business benefits from marketing automation…

What Program Can Open an XLS and XLSX File? Equivalents of EXCEL

Read about alternative tools for viewing and editing Excel tables. In spite of the huge popularity of Microsoft Excel, many users still ask the question “what to use for opening XLS and XLSX files.”…