Make Copy of Linked List in Python (2 Examples)
In this tutorial, you’ll learn how to make a copy of a linked list in Python. A linked list is a data structure where each element (node) contains a value and a reference to the next node. We’ll explore different methods to create a copy of a linked list.
The table of contents is structured as follows:
Let’s dive into it!
Initializing Sample Linked List and Importing Modules
Firstly, we will import the copy module, then we will define a linked list. To do that, we will implement a basic Node class. Afterward, we will create a couple of nodes and link them to each other.
import copy class Node: def __init__(self, value): self.value = value self.next = None # Linked List head = Node(1) second = Node(2) third = Node(3) head.next = second second.next = third
Example 1: Creating Shallow Copy
The first example involves creating a shallow copy of a linked list. Here’s how it works:
# Shallow copy new_head = head # Print original linked list current_node = head while current_node: print(current_node.value) current_node = current_node.next # 1 # 2 # 3 # Print shallow copy current_node = new_head while current_node: print(current_node.value) current_node = current_node.next # 1 # 2 # 3 # Deleting a node from original linked list head.next = third # Print original linked list current_node = head while current_node: print(current_node.value) current_node = current_node.next # 1 # 3 # Print shallow copy current_node = new_head while current_node: print(current_node.value) current_node = current_node.next # 1 # 3
In this example, we define a Node
class to represent each element of the linked list. We create a linked list with three nodes and set the references accordingly. To create a shallow copy, we assign the head
node to a new variable new_head
. Both variables point to the same memory location, so any modification to one will reflect in the other. When we delete a node from the original linked list, it affects both the original and shallow copy.
Example 2: Creating Deep Copy
The second example demonstrates creating a deep copy of a linked list. Since our initial linked list was modified, we will first re-initialize it, then perform the deepcopy operation Here’s the example:
# Re-initializing the Linked List head = Node(1) second = Node(2) third = Node(3) head.next = second second.next = third # Deep copy new_head = copy.deepcopy(head) # Print original linked list current_node = head while current_node: print(current_node.value) current_node = current_node.next # 1 # 2 # 3 # Print deep copy current_node = new_head while current_node: print(current_node.value) current_node = current_node.next # 1 # 2 # 3 # Deleting a node from original linked list head.next = third # Print original linked list current_node = head while current_node: print(current_node.value) current_node = current_node.next # 1 # 3 # Print deep copy current_node = new_head while current_node: print(current_node.value) current_node = current_node.next # 1 # 2 # 3
In this example, we use the deepcopy() function from the copy module to create a deep copy of the linked list. The deep copy creates a new linked list with separate nodes, so the modifications to one list don’t affect the other. Even after deleting a node from the original linked list, the deep copy remains unchanged.
Video, Further Resources & Summary
Do you need more explanations on making a copy of a linked list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- List in Python (Beginner & Advanced)
- Circular Doubly Linked List in Python
- Delete Node in Linked List in Python (Example)
- Loop Through List of Strings in Python
- Loop Over Range List in Python
- Loop Through List of Integers in Python
This post has shown how to make a copy of a linked list in Python using both shallow and deep copy methods. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter