Random Singly Linked List Generator using Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to generate a singly linked list using random numbers. Prerequisite: Insertion in Linked List A Singly Linked List is a linear data structure. A linked list is a collection of nodes and in a singly linked list every node contains two things: Data Reference to the next node In this article, we will learn to generate a Singly Linked List without asking for data from the user. Generating a Singly Linked List of random length:Import Modules Python import random Define Node class Python3 # Node class class Node: # constructor to initialize the node object def __init__(self,data): self.data = data self.next = None Define Singly Linked List class: In this class, we defined two methods, one is to insert an element at the end of the Linked List and the other method is to print Linked List.tail variable is to store last node of Singly Linked List, this help us to insert element without traversing whole linked list again and again. By using this variable complexity of insertion is reduced to O(1) form O(n). Python3 class Singly_Linked_List: # constructor to initialize the Singly_Linked_List object def __init__(self): self.head = None self.tail = None def insert_at_last(self, data): # create temporary node new_node = Node(data) # if head is None, it means that Singly_Linked_List is empty and therefore tail is also None if self.head == None: # make head to new node that is inserted self.head = new_node # at this point Singly_Linked_List contains only 1 node , therefore head and tail both will point to it self.tail = new_node else: # Insert new node at the end of Singly_Linked_List self.tail.next = new_node # update tail of Singly_Linked_List after inserting node self.tail = new_node def print_Singly_Linked_List(self): t_head = self.head print("\n Our Singly_Linked_List is : ", end="") while(t_head): print(t_head.data, end=" -> ") t_head = t_head.next print("Null \n") Define lambda function to generate and return a random number. Start and end are variables that define the range of numbers that will be generated by the lambda function. Python3 start = 0 end = 20 random_int = lambda : random.randint(start,end) Define the Main function Python3 def main(): linked_list = Singly_Linked_List() for i in range(random_int()): linked_list.insert_at_last(i) linked_list.print_Singly_Linked_List() if __name__ == "__main__": main() Complete Code: This code generates a Singly Linked List of random size (size is between 0 and 20) and the data of each node is its index. Python3 import random # Node class class Node: # constructor to initialize the node object def __init__(self,data): self.data = data self.next = None # Singly Linked List class class Singly_Linked_List: # constructor to initialize the Singly_Linked_List object def __init__(self): self.head = None self.tail = None def insert_at_last(self,data): # create temporary node new_node = Node(data) # if head is None, it means that Singly_Linked_List is empty and therefore tail is also None if self.head == None: # make head to new node that is inserted self.head = new_node # at this point Singly_Linked_List contains only 1 node , therefore head and tail both will point to it self.tail = new_node else: # Insert new node at the end of Singly_Linked_List self.tail.next = new_node # update tail of Singly_Linked_List after inserting node self.tail = new_node def print_Singly_Linked_List(self): t_head = self.head print("\n Our Singly_Linked_List is : ",end="") while(t_head): print(t_head.data,end=" -> ") t_head = t_head.next print("Null \n") start = 0 end = 20 random_int = lambda : random.randint(start,end) def main(): linked_list = Singly_Linked_List() for i in range(random_int()): linked_list.insert_at_last(i) linked_list.print_Singly_Linked_List() if __name__ == "__main__": main() Output: Generating a Singly Linked List of random length where data in every node is a random number:For doing this we need to define a new lambda function to generate a random number and use it in the main function while inserting a node in the Singly Linked List. Python3 # random_data will return a number between 0 and 100000 random_data = lambda : random.randint(0,100000) def main(): linked_list = Singly_Linked_List() for i in range(random_int()): linked_list.insert_at_last(random_data()) linked_list.print_Singly_Linked_List() Code: Python3 import random # Node class class Node: # constructor to initialize the node object def __init__(self,data): self.data = data self.next = None # Singly Linked List class class Singly_Linked_List: # constructor to initialize the Singly_Linked_List object def __init__(self): self.head = None self.tail = None def insert_at_last(self,data): # create temporary node new_node = Node(data) # if head is None, it means that Singly_Linked_List is empty and therefore tail is also None if self.head == None: # make head to new node that is inserted self.head = new_node # at this point Singly_Linked_List contains only 1 node , therefore head and tail both will point to it self.tail = new_node else: # Insert new node at the end of Singly_Linked_List self.tail.next = new_node # update tail of Singly_Linked_List after inserting node self.tail = new_node def print_Singly_Linked_List(self): t_head = self.head print("\n Our Singly_Linked_List is : ",end="") while(t_head): print(t_head.data,end=" -> ") t_head = t_head.next print("Null \n") start = 0 end = 20 random_int = lambda : random.randint(start,end) # random_data will generate random number between 0 and 100000 random_data = lambda : random.randint(0,100000) def main(): linked_list = Singly_Linked_List() for i in range(random_int()): linked_list.insert_at_last(random_data()) linked_list.print_Singly_Linked_List() if __name__ == "__main__": main() Output: Comment More infoAdvertise with us Next Article Random Singly Linked List Generator using Python 06akshay2002 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads Singly Linked List in Python A Singly Linked List is a type of data structure that is made up of nodes that are created using self-referential structures. Each node contains a data element and a reference (link) to the next node in the sequence. This allows for a dynamic and efficient management of data elements. Table of Conte 10 min read Generating random Id's in Python Generating random IDs in Python is useful when we need unique identifiers for things like user accounts, sessions, or database entries. In this article we will see Various methods to Generate Random ID's in Python.Using random.randint()random.randint() method in Python is used to generate random int 2 min read Random Binary Tree Generator using Python A binary tree is a tree data structure where each node has at most two children, known as the left child and the right child. A random binary tree is a binary tree that is generated randomly with a certain number of nodes. Random binary trees are commonly used for testing algorithms and data structu 7 min read How to build a Random Story Generator using Python? In this section, we are going to make a very interesting beginner-level project of Python. It is a random story generator. The random story generator project aims to generate random stories every time user executes the code. A story is made up of a collection of sentences. We will choose random phra 4 min read How to generate a random phone number using Python? In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with 1 min read Create a Random Password Generator using Python In this article, we will see how to create a random password generator using Python. Passwords are a means by which a user proves that they are authorized to use a device. It is important that passwords must be long and complex. It should contain at least more than ten characters with a combination 5 min read Randomly Select N Elements from List in Python When working with lists in Python, we often need to randomly select a specific number of elements. For example, consider the list a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]. We might want to randomly select 3 elements from this list. Let's discuss different ways for selecting n random elements fr 3 min read Python - Generate random numbers within a given range and store in a list Our task is to generate random numbers within a given range and store them in a list in Python. For example, you might want to generate 5 random numbers between 20 and 40 and store them in a list, which could look like this: [30, 34, 31, 36, 30]. Let's explore different methods to do this efficientl 3 min read Reverse a Linked List - Python Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes.Examples: Input: head: 1 -> 2 -> 3 -> 4 -> NULLOutput: head: 4 -> 3 -> 2 -> 1 -> NULLExplanation: Reversed Linked List:Reversed 3 min read Select random value from a list-Python The goal here is to randomly select a value from a list in Python. For example, given a list [1, 4, 5, 2, 7], we want to retrieve a single randomly chosen element, such as 5. There are several ways to achieve this, each varying in terms of simplicity, efficiency and use case. Let's explore different 2 min read Like