In this tutorial, we are going to learn the python program to create a singly linked list and traverse its elements.
Problem Statement
Our program will take the elements of the linked list from the user as the input and then print the elements of the linked list.
For example
Case1: If the user inputs the elements of the linked list as 1, 2, 3, 4.
The output should be 1234.
Case2: If the user inputs the elements of the linked list as 7, 9, 1, 0.
The output should be 7910.
What is a Linked List?
A linked list is the collection of elements that are connected via the memory address of successor elements. It is an unordered collection of data that is connected via links. Each data elements have another block that contains the address of the next elements of the linked list.
Creation Of Linked List:

The singly linked list is a linear records shape wherein every element of the listing incorporates a pointer which factors to the following detail inside the listing. Each element within the singly connected list is referred to as a node. Each node has two components: data and next pointer, which factors the following node within the listing. The first node of the list is the head, and the closing node of the list is called a tail. The last node of the list contains a pointer to the null. Each node in the list can be linearly accessed with the help of traversing through the list from head to tail. The linked list is created using the node class. We create a node object and another class to use this node object.
Traversal of a linked list
Traversing is the most common operation of the linked list, it means visiting every node of the linked list once and performing any operation usually, we insert, delete or display the elements of the linked list. For traversing basic idea is to visit every node from head to the null value.

Our Logic to create and traverse singly linked list
- Our program will take a list of elements as input from the user.
- Store the elements inputs by the user in linked list format(Linked list creation)
- Traverse through each element of the linked list and print the elements as the output (Traverse of linked list)
Let’s code the above algorithm in the python programming language.
Python Program to create and traverse singly linked list
#Creating node class of the singly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
self.tail = None
#Creating addNode() to add newly created nodes.
def addNode(self, data):
if self.tail is None:
self.head = Node(data)
self.tail = self.head
else:
self.tail.next = Node(data)
self.tail = self.tail.next
#Creating display() to print newly created nodes via traversing.
def display(self):
current = self.head
while current is not None:
print(current.data, end = ' ')
current = current.next
s = SinglyLinkedList()
n = int(input('enter the number of elements in linked list : '))
for i in range(n):
data = int(input('Enter data: '))
s.addNode(data)
print('The linked list: ', end = '')
s.display()
Output
enter the number of elements in linked list : 4
Enter data: 1
Enter data: 2
Enter data: 3
Enter data: 4
The linked list: 1 2 3 4
Explanations
For the input size 4, the data elements of the linked list are input as 1, 2, 3, and 4. The elements are connected using the pointer which is the pointer to the next node.
1–>2–>3–>4–>Null
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…