create alinked list inb pyhton

class Node:
    def __init__(self, data = None, next_node = None):
        self.data = data
        self.nextNode = next_node

    def get_data(self):
        return self.data

    def set_data(self, data):
        self.data = data

    def get_nextNode(self):
        return self.nextNode

    def set_nextNode(self, nextNode):
        self.nextNode = nextNode


class LinkedList:
    def __init__(self, head = None):
        self.head = head


    def add_Node(self, data):
        # if empty
        if self.head == None:
            self.head = Node(data)


        # not empty
        else:
            curr_Node = self.head
            
            # if node added is at the start
            if data < curr_Node.get_data():
                self.head = Node(data, curr_Node)
                
            # not at start
            else:
                while data > curr_Node.get_data() and curr_Node.get_nextNode() != None:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()

                # if node added is at the middle
                if data < curr_Node.get_data():
                    prev_Node.set_nextNode(Node(data, curr_Node))
                

                # if node added is at the last
                elif data > curr_Node.get_data() and curr_Node.get_nextNode() == None:
                    curr_Node.set_nextNode(Node(data))



    def search(self, data):
        curr_Node = self.head
        while curr_Node != None:
            if data == curr_Node.get_data():
                return True

            else:
                curr_Node = curr_Node.get_nextNode()

        return False


    def delete_Node(self, data):
        if self.search(data):
            # if data is found

            curr_Node = self.head
            #if node to be deleted is the first node
            if curr_Node.get_data() == data:
                self.head = curr_Node.get_nextNode()

            else:
                while curr_Node.get_data() != data:
                    prev_Node = curr_Node
                    curr_Node = curr_Node.get_nextNode()
                    
                #node to be deleted is middle
                if curr_Node.get_nextNode() != None:
                    prev_Node.set_nextNode(curr_Node.get_nextNode())

                # node to be deleted is at the end
                elif curr_Node.get_nextNode() == None:
                    prev_Node.set_nextNode(None)

        else:
            return "Not found."

    def return_as_lst(self):
        lst = []
        curr_Node = self.head
        while curr_Node != None:
            lst.append(curr_Node.get_data())
            curr_Node = curr_Node.get_nextNode()

        return lst

    def size(self):
        curr_Node = self.head
        count = 0
        while curr_Node:
            count += 1
            curr_Node = curr_Node.get_nextNode()
        return count

      
## TEST CASES #
test1 = LinkedList()
test2 = LinkedList()
test1.add_Node(20)
test1.add_Node(15)
test1.add_Node(13)
test1.add_Node(14)
test1.delete_Node(17)
print(test1.return_as_lst())
print(test2.size())

4.25
4

                                    nothing

4.25 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
linked list in python implementation python built in linked list linked list in python best explanation how to make linked list on python define linked list in python does python have linked lists is a python list a linked list linked list python uses linked list in python with explanation create linked list by python how to build linked list with a list python implement linkedlist python are linked lists needed in python linked list py linked list program in python whole code linked list implementation using python linked list python explained what are linked list pyhton linked list python example define a linked list in python syntax of create linked list in python how to create linked list in python is linked list in python python example linkedlist does python use linked lists python list is linked list linked list using list in python basic linkedlist operations in python linked list in python3 linked list implementation inpython linked list python linked list does python have a built in linkedlist what is a linked list python linked list sing python linked list python tutorial linked list i python in linked list python linked list python using python to define a linked list linked list on python how to import linked list in python linkedlist using python how to code a linked list in python built in linked list python implementing linked list in python make linked list in python linked list code python linked list python implementation linked list implementation in python how to use builtin linked list in python linkedin list python python linked list functions python linked-list linked list in python using list implement a linked list in python linked list in py linked list in python example linked list pyrhon linked list python code linked list pytohn] linked list python module linked list pyhton linked lsit in python what are linked lists? called in python does python have a built in linked list implementing a linked list in python linked list in pyton how do linked lists work in python how to make linked lists python python implement linked list do python list act as linked list linkedlists in python linked list using python linked list methods python linkedlist python tutor python linkedlist library are list in python linked lists import linked list python linked list in python library python library for linked list how to define a linked list in python linked list python methods linked list with python linked-list python in detail linked-list python how to make a linked list in python python linked list how to linked lists python code display linked list in python linked list object python linked list python example code linked list python program what is linked list in python python linked list library linked list python linkedlist how to use linked list in python linked list in python tutorialpoinnt python chained linked list how to fail linked list contains in python how to create a new linked list python code for creating linked list in python how to make linked list object python how to values of a linked are in another linked list python python linked code python initilize linked list python sigly linked list in python dynamic singly linked list program in python singly linked list program in python linked list code in python how to linked lists together python how to link lists together python linkedlist python traverse linked list in python linked list program in python linked list method in python python linkeidn list python linked list how to keep track of head python linked list implementation python create linked list linked list node class python linked list implementation in python link list in python chained list python lnked list defautl data structrei n python self in linkedlist python how to return the head of a linked list in python python linked list linked lists python3 linked list traversal in python singly linked operations in data structure code python create alinked list inb pyhton create a linkedlist in python python for in linked list store a number in linked list python read linked list python linked list insertions python algorithm linled linsertions python algorithm create linked list in python traverse linked list python how to define a linked list python linked lists in python data structures and algorithms linked list python linked list algorithm in python List nodes in python how to create a linked list in python are python lists linked lists list in python is singly linked queue linked list python next.val linkied list are linked lists used in python how to create linked list in python how to create a linked list in python3 bound to a list python what is how to make linked list in python list implementation in python linkedlist in python python create a simple linked list listnode in python accesing linked list python singly linked list python linked list all operations in python how to create linked list python list node python doubly linked list in python what is a linked list .python python linkedlist implementation linkedin list in python linked list python3 linke list in python initialize linked list node in python singly linked list pythin linked list python linked list operations in python python linked lists linked lists python python program for linked list python doubly linked list python linked list head method linked lists in python notes linked list pythin ADVANTAGES OF LINKED LISTS IN PYTHON linked chain implementation in python python traverse a linked list what does a linked list output look like in python for test definition of a function in python linked list linked list example python Python how to create a linked list single linked list in python python class linked list py linked list one way linked list python how to create a node in linked list python what does a linked list look like in python python insert into linked list return whole list create linkedlist using list python creation of linked list in python List to linked list Python define a loinked list in python python linked list sequence list de link python linked lists in python 3 linklist python List python linked list linkedlist function in python linked lists python 3 implementation of all the methods in linkedlist in python linked list in pythoon howto linked list in python linked list implementation python how to display pointers in a linked list in python python linked list methods inbuilt function for head for linked list in python singly-linked list python traversing a linked list python implement linked list in python how to make a linked lit in python with pointers learn how to implement linked list in python create linked list python linked list traversal python traverse a linked list python populating an empty linked list in python how to traverse a linked list in python python source code Linked list python adding t linked lists when to use linked list in python CREATE A linked list python is there linked list in python understanding linked lists python linked list tutorial python python new linked list python return linked list how to work with linked lists python linkelist in python singly linked list python example single linked list python example linked lists in python3 how to use a linked list in python singly linked list using python linkjed list python python3 linked list linkd list python in python linked list python linked list linked list python linked list in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source