LinkedList in Python

In this post, we'll get to know how we can implement LinkedList in Python.

#singly LinkedList
# forst we'll create a class to create nodes

class Node:
    def __init__(self,data=None, next=None):
        self.data=data      #assigning data to the data variable
        self.next=next      #initializing next node as null (None in python)

# creating class for linked list
class LinkedList:
    def __init__(self):
        self.head=None   #initializing head pointer
   
    def insert_at_beginning(self, data):
        node=Node(data,self.head)      #next will point to head of next node
        self.head=node                #head will point to new node which is inserted at beginning

    def insert_at_end(self,data):
        # if L_list is empty insert at beginning
        if self.head is None:
            self.head=Node(data, None)
            return
        itr=self.head
        while itr.next:
            itr=itr.next
        #at the end of the while loop, itr.next will be pointing to None
        # that means we reached the end of the linkedlist
        # now we'll create a new node from our given data and will assign to the itr.next
        # that means itr.next will point to our node
        itr.next=Node(data, None)      

    def insert_values(self, data_list):
        # here we'll be converting list in LinkedList
        self.head=None #wiping out existing data
        for data in data_list:
            self.insert_at_end(data)  

    def get_len(self):
        # we'l count the total no of heads present in the LL
        count=0
        itr=self.head
        while itr:
            count+=1
            itr=itr.next  
        return count      

    def remove_at_index(self,index):
        if index<0 or index>=self.get_len():
            # raise Exception("Invalid Index")
            print('Invalid Index')
            return
        if index==0:
            self.head=self.head.next
            return

        itr=self.head
        count=0
        while itr:
            if count==index-1:
                itr.next=itr.next.next
                break
            itr=itr.next
            count+=1            
    def insert_at_index(self, index, data):
        if index<0 or index>=self.get_len():
            # raise Exception("Invalid Index")
            print('Invalid Index')
            return
        if index==0:
            self.insert_at_beginning(data)
            return
        itr=self.head
        count=0
        while itr:
            if count==index-1:
                temp=itr.next
                itr.next=Node(data, temp)
                del temp
                break
            itr=itr.next
            count+=1            


    def print_LL(self):
        if self.head is None:
            print("LinkedList is empty")
        else:
            itr=self.head
            ll_str=""
            while itr:
                ll_str+=str(itr.data)+"-->"
                itr=itr.next
            print(ll_str)

if __name__=="__main__":
    ll=LinkedList()
    ll.insert_values([7,5,2,63,15,98,12])
    ll.remove_at_index(1)
    ll.insert_at_index(2, 55)
    ll.print_LL()

Comments

Popular Posts