Creating a Linked List using Insert

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 183:-  Creating a Linked List using Insert

 

Sure! We can create a linked list by using the insert method to add elements to the list one by one. Below is an example of how you can create a linked list using insertions:

class Node:    def __init__(self, data):        self.data = data        self.next = None class LinkedList:    def __init__(self):        self.head = None    def insert(self, data):        new_node = Node(data)        new_node.next = self.head        self.head = new_node    def display(self):        current = self.head        while current:            print(current.data, end=" -> ")            current = current.next        print("None") # Example usage: if __name__ == "__main__":    linked_list = LinkedList()    # Insert elements to create the linked list    linked_list.insert(3)    linked_list.insert(7)    linked_list.insert(1)    linked_list.insert(9)    # Display the linked list    print("Linked List:")    linked_list.display()

In this example, we first define the Node and LinkedList classes. The LinkedList class has an insert method that inserts elements at the beginning of the linked list. We then create a linked list object, and using the insert method, we add elements (3, 7, 1, and 9) to create the linked list. Finally, we display the created linked list.

The output will be:

rust

Copy code

Linked List: 9 -> 1 -> 7 -> 3 -> None

The linked list is created in reverse order due to inserting elements at the beginning, resulting in the order 9 -> 1 -> 7 -> 3.

10. Linked List

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support

Sciaku (सियाकु)

Sciaku (सियाकु) provides you a technical and programming content like Java Programming, Python Programming, C Programming,Android Development, Web Development, etc. Learn how to make software, website, and applications here and also we have industrial internship for you.

Contact

G20, Gopal Vihar Colony, Noida Sector 2, Uttar Pradesh, India, 201301

info@sciaku.com

Copyright © 2022-2025 Created by ❤️ Sciaku

Privacy Policy | Terms & Conditions | Refunds Policy