linked list python example

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())

0
0
LJ Writer 95 points

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

class SLinkedList:
    def __init__(self):
        self.head = None

    def Atbegining(self, data_in):
        NewNode = Node(data_in)
        NewNode.next = self.head
        self.head = NewNode
		
# Function to remove node
    def RemoveNode(self, Removekey):

        HeadVal = self.head

        if (HeadVal is not None):
            if (HeadVal.data == Removekey):
                self.head = HeadVal.next
                HeadVal = None
                return

        while (HeadVal is not None):
            if HeadVal.data == Removekey:
                break
            prev = HeadVal
            HeadVal = HeadVal.next

        if (HeadVal == None):
            return

        prev.next = HeadVal.next

        HeadVal = None

    def LListprint(self):
        printval = self.head
        while (printval):
            print(printval.data),
            printval = printval.next


llist = SLinkedList()
llist.Atbegining(&quot;Mon&quot;)
llist.Atbegining(&quot;Tue&quot;)
llist.Atbegining(&quot;Wed&quot;)
llist.Atbegining(&quot;Thu&quot;)
llist.RemoveNode(&quot;Tue&quot;)
llist.LListprint()

0
0
4.14
7
Rmwpg 120 points

                                    pip install llist

4.14 (7 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
linkedinlist python code implement a linked list in pythonb how create linked list in python python built in linked list linked list in python best explanation how to make linked list on python how to make a linked list in pytohn define linked list in python create a linked list in python example does python have linked lists can you import a linked list in python is a python list a linked list linked list python uses linked list in python with explanation linked list and machine learning python create linked list by python how to build linked list with a list python implement linkedlist python import linked list pythobn python example of a linked list linked list node in python are linked lists needed in python linked list program in python whole code linked list implementation using python linked list python explained what are linked list pyhton define a linked list in python syntax of create linked list in python how to syntax create linked list in python how to create linked list in python is linked list in python creating linked list in python python linkedlist nodes python example linkedlist linked list in python programiz python list is linked list make a linked list python linkedlists library in python what are linked lists in python linked list using list in python basic linkedlist operations in python linked list implementation inpython linked list python linked list does python have a built in linkedlist linked list sing python linked list python tutorial linked list concept in python in linked list python linked list python using python to define a linked list linkedlist using python linked list on python create python linkedlist how to import linked list in python how to code a linked list in python built in linked list python 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 linked list in py linked list pyrhon linked list project in python linked list pytohn] python linked list documentation linked list python module python making linked lists 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 program to demonstrate linked list in python 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 are there built in methods for linked lists in 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 with python linkedlist python full code linked-list python in detail linked-list python python linked list how to linked lists python code display linked list in python linked list object python linked list python program linked list and its operations code in python how to use a linked list in python python linked list library linked list example pytohn linked list py create a linked list in python with methods linked list tutorial python linked list python library linked list linked list in python inbuilt all functions you can do to linked lists python Python 3 linked list list using linked list in python linked lsit python how to use linked lists in python use of linked list in python linked list code python linkedlist from linkedlist python linked list pyhton python linked list examples example of linked list in python linked list implementation of python using .link in linked list in python linked list python example code linked list in python example linked list in python tutorialpoinnt linked list in python program python list linked list? python linked lists .link linked list = linked list python how to call linked list in python build a linked list python simple linked list in python linked list methods python python make a linked list python function linked list linked list example python python learn linkedlist Linked list in python python program to create linked list how to use linked list in python 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 how to save elements of a linked list to a queue in python sigly linked list 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 example of using of linked lists python traverse linked list in python linked list method in python python linkeidn list python linked list how to keep track of head linked list node class 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 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 data structures and algorithms linked list python linked list algorithm in python List nodes in python are python lists linked lists list in python is singly linked queue linked list python Linkedin URLs list lython linked list prev in python node list class python Implementation linked list in python making a linked list in python insert into linked list python linked list in python tutorial singly linked list implementation in python list node to list python implement linked list python python code for singly linked list next.val linkied list learn linked list python python list linked list python 3 linked list functions how to create linked list in python does python use linked lists how to create a linked list in python3 python linked link bound to a list python what is how to make linked list in python list implementation in python linked list python example linke list pythi python build a linked list linkedlst in python how to make the linked list in python python create a simple linked list list node in python listnode in python node in python accesing linked list python python linked list insert declaring a linked list in python linked list all operations in python Working With Linked Lists in Python linked list in python implementation List and ListNode python adding elements of a linkedlist python linked list operations python how to create linked list python list node python can i use linked lists in python python how to inmplement linked list adding to a linked list python how to create a linked list in python 3 linked list in django tutorialspoint linked list types in python doubly linked list in python how to linked list different from normal list python linked lists python 3 what is a linked list .python python store number as linked list python linkedlist implementation linked lis in Pythong make linked list in python using one class only implementing linked lists in python linked lists neet lecture notes python linkedin list in python linked list in tkinter linked list python3 linke list in python linked list inpython create new node in linked list pyhton create a linkedlist python linked list i python linked lit class in python same object in linked list pyhton initialize linked list node in python PYTHON LINKED LISTS' single linked list python singly linked list pythin linked list python python program for linked list python doubly linked list python linked list head method linked list put python 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 Python how to create a linked list code a linked list python single linked list in python python class linked list python linked list tutorial 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 python linked list insertion python linked list node 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 what is list node in python python linked list methods inbuilt function for head for linked list in python singly-linked list python traversing a linked list python how to make a linked lit in python with pointers learn how to implement linked list in 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 python linked list implementation 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 singly linked list using python linkjed list python python LinkedList class python3 linked list linkd list python in python linked list how to define a linked list python linked list in python step by step set linked list pointer pytho working with listnodes in python singly linked list python python linked list to list pythonds.basic linked list python linked lists make a linked list in python linked list functions python linked lists python3 tutorial linked lists python tutorial python linked list in python python inked list inserting data structure list in flask sqlalchemy linkedlist in python3 python linked class how to declare linked list in python condense linked list python how to go back to beginigng of linkedlist python python as linked list linked list by functions in python python liked list code link list in python linked list python are called implement linked list in python linked list python methods create a linkedlist in python linked list itr python python create linked list is linked list is useful for python python list node linkedlist in python linked list in python for beginners implementing linked list in python make a list a linked list python linked list python code create linked list in python how to implement a linked list in python python how to make a linked list implement a linked list in python linked list functions in python linklist in python data structure linked list in python traversing singly linked list python lista implementation in python python create a linked list with a list create linked list python linked list program in python linked list methods in python w2 linked list methods in python linked list operations in python linked list operationsin python get() linked list python python linked list structure linked list implementation in python linked list python 3 what is a linked list python linkedlist in python very simple how to create a linked list in python linked list in python3 how to implement linked list in python linked list in python code data structures linked list python python linked list data structure class is it easy to learn linked list in python what is a linked list in python how to access the elements of a linked list in python linked lists are written in python linked lists in python what are the built in linked lists in python? understanding linked lists in python create a linked list in python linked lists python python linked list code singly linked list in python link list python example python linkedlist linked list python implementation python singly linked list traverse linked list python python link list are linked lists used in python is a python dictionary a linked list python linked list class best way to implement a linked list python linked list class code python linked list class python insert between linked list python why do I do python linked list how do I do python linked list if self.head linkedlist python how to make a linked list in python linked list python linkedlist python how to form the linkedlist in python linked list using python how to create a linked list python python linked list what is linked list in python make linked list in python most common singly linked list class 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