Convert List to Linked List & Vice-Versa in Python (2 Examples)

 

Hi! This tutorial will show you how to transform a list into a linked list and vice-versa in the Python programming language.

Here is an overview:

Let’s jump into the Python code!

 

Create Example List

Here, we will create an example Python list of integer values that we will change into a linked list and vice-versa in this tutorial.

Therefore, in your preferred Python programming IDE, run the code below to create the example list:

# sample list
my_list = [1, 2, 3, 4, 5] 
 
# print my_list
print(my_list)
# [1, 2, 3, 4, 5]
 
# print type of my_list
print(type(my_list))
# <class 'list'>

 

Example 1: Transform List to Linked List

In this first example, we will convert the list into a linked list. Therefore, run the code below:

# create linked list
class NodeList:
  def __init__(self, val = 0, next = None):
    self.val = val
    self.next = next
 
# create first node as head
head = NodeList(my_list[0])
current = head
 
# loop through list to create nodes and link them together
for item in my_list[1:]:
  current.next = NodeList(item)
  current = current.next
 
# print linked list
current = head
while current is not None:
  print(current.val)
  current = current.next
# 1
# 2
# 3
# 4
# 5

In the above example, we start by defining a NodeList class representing individual elements of the linked list, with each node having a val attribute to store the element and a next attribute to point to the next node.

Next, we create the first node, called the head, using the first element of my_list. After creating the first node and designating it as the head of the linked list, current is initialized to point to the same node. This is a common practice in linked list traversal, where the current node is used to iterate through the linked list.

Then we iterate through the remaining elements in my_list. Inside the for loop, a new node is created using NodeList(item) with the current item from the iteration. This node is intended to represent the next element in the linked list. It’s attached as the next attribute of the current node. This essentially connects the current node to the newly created node, extending the linked list.

After connecting the current node to the newly created node, current is updated to point to the newly created node through current = current.next. This ensures that in the next iteration of the loop, current will refer to the newly added node, allowing the loop to continue adding nodes to the end of the linked list.

Finally, we print the elements of the linked list by traversing it from the head to the end, demonstrating the conversion of the Python list into a linked list and the ability to access its elements sequentially.
 

Example 2: Transform Linked List to List

In this next example, we will change the linked list back into a list:

# create empty list
new_list = []
 
# start from head 
current = head
 
# traverse through linked list
while current is not None:
  new_list.append(current.val)
  current = current.next
 
# print new_list
print(new_list)
# [1, 2, 3, 4, 5]

Here, we create a new empty list called new_list. Then we use a while loop to traverse the linked list, starting from the head node and moving through the list node by node.

During each iteration of the loop, we append the value (denoted as current.val) of the current node to new_list. This process continues until the end of the linked list is reached, at which point the loop terminates.

Finally, the contents of new_list are printed using the print() function, effectively converting the elements of the linked list into a Python list.
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a list into a linked list and vice-versa in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a list into a linked list and vice-versa in Python.

 

The YouTube video will be added soon.

 

With that, we have demonstrated how to convert a list to a linked list and vice-versa in Python. Furthermore, you could have a look at some of the other interesting Python list tutorials on Statistics Globe:

This post has shown how to convert a list into a linked list and vice-versa in Python. I hope you enjoyed reading this post! In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top