Program to delete a linked list

 In this program we will see how we can delete a linked list


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

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

    def display(self):
        temp = self.head
        while temp!=None:
            print(temp.data, end="-->")
            temp = temp.next
    def deleteLinkedList(self):
        while self.head!=None:
            temp =self.head
            self.head = self.head.next
            del temp

    def length(self):
        temp = self.head
        count = 0
        while temp!=None:
            count = count+1
            temp = temp.next
        return count
if __name__=="__main__":
    llist = LinkedList()
    first = Node(10)
    second = Node(20)
    third = Node(30)
    fourth = Node(40)
    fifth = Node(50)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    llist.display()
    llist.deleteLinkedList()
    print('\nAfter delete')
    llist.display()
    

To print the middle term of a linked list

 In this program we will print the middle term of a linked list

If there is odd number of nodes then middle term will be only one number


Input: 10=>20=>30=>40=>50=>
Output: 30

But if there is even number of nodes then there will be two middle term so in such case we will print the second middle term


Input: 10=>20=>30=>10=>40=>50=>
Output: 10


Program:

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

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

    def length(self):
        temp = self.head
        count = 0
        while temp!=None:
            count = count+1
            temp=temp.next
        return count
    def middle(self):
        length1 = self.length()
        
        if length1%2==0:
            flag = (length1/2)+1
            return self.nthNode(flag)
        else:
            flag = (length1+1)/2
            return self.nthNode(flag)

    def nthNode(self,index):
        temp=self.head
        count = 1
        while temp!=None:
            if count==index:
                
                return temp.data
                
            else:
                temp=temp.next
                count = count+1
                
        
if __name__=="__main__":
    llist = LinkedList()
    first = Node(10)
    second = Node(20)
    third = Node(30)
    fourth = Node(10)
    fifth = Node(50)
    sixth = Node(60)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    fifth.next = sixth
    print(llist.middle())

To check if the linked list is palindrome

 In thisprogram we will see how to check if the linked list is palindrome or not.

Input: 10=>20=>30=>20=>10
Output: True

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

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


    def length(self):
        temp = self.head
        count = 0
        while temp!=None:
            count = count+1
            temp=temp.next
        return count
    
    def IterativeSearch(self,index):
        temp = self.head
        count = 1
        len = self.length()
        while temp!=None:
            #print("This loop is running for",temp.data,"and value of count at this pos is",count)
            if count == index:
                #count=count+1
                return temp.data
            else:
                count=count+1
                temp = temp.next
        if count>len:
            return 0
    
    def checkPalindrome(self):
        len = self.length()
        flag = 0
        if len%2==0:
            flag = len/2
        else:
            flag = (len-1)/2
        start_counter = 1
        counter = 0
        while start_counter<=flag:
            if self.IterativeSearch(start_counter)==self.IterativeSearch(len) :
                counter=counter+1
                start_counter = start_counter+1
                len = len-1
            else:
                return False
        if counter==flag:
            return True

if __name__=="__main__":
    llist = LinkedList()
    first = Node(10)
    second = Node(10)
    third = Node(10)
    fourth = Node(10)
    fifth = Node(10)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    print(llist.checkPalindrome())

Nth node from end in a Linked List

 In this program we will see how to get nth node from the end in a Linked List.


linked list: 20=>40=>30=>80=>90=>
Input: 2
Output: 80


class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
class LinkedList:
    def __init__(self):
        self.head = None
    def display(self):
        temp = self.head
        while temp!=None:
            print(temp.data, end="=>")
            temp = temp.next
    def length(self):
        temp = self.head
        count=0
        while temp!=None:
            count = count+1
            temp = temp.next
        return count
    def NthNodeFromEnd(self,index):
        if index<0:
            print('Enter correct value')
        else:
            count = LinkedList.length(self)- index
            if count<0:
                print('Not a Valid index')
            else:
                #print('You have to print value', count)
                temp = self.head
                for i in range(count):
                    temp = temp.next
                print("Your data is ", temp.data)
if __name__=="__main__":
    #20=>40=>30=>80=>90=>
    llist = LinkedList()
    first = Node(20)
    second = Node(40)
    third = Node(30)
    fourth = Node(80)
    fifth = Node(90)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    llist.display()
    print("\nLength: ",llist.length())
    llist.NthNodeFromEnd(2)


Output:



            


Insertion of element at given position in Linked List

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

class LinkedList:
    def __init__(self):
        self.head = None
    
    def display(self):
        print()
        temp = self.head
        while temp!=None:
            print(temp.data, end="-->")
            temp = temp.next

    def InsertATNode(self,data,index):
        dataADD = Node(data)
        pos = 1
        temp1 = self.head
        temp2 = temp1.next
        if index == pos:
            dataADD.next = temp1
            self.head = dataADD
        else:
            if index<=0:
                print('\nEnter a valid index')
            while index!=0:
                index = index-1
                if index == 1:
                    dataADD.next = temp2
                    temp1.next = dataADD
                    break
                elif index > LinkedList.length(self):
                    print('\nSize is out of index')
                    break
                else:
                    temp1 = temp1.next
                    temp2 = temp1.next
                    pos=pos+1
            
    
    def length(self):
        temp = self.head
        count = 0
        while temp!=None:
            count=count+1
            temp = temp.next
        return count

if __name__ == "__main__":
    llist = LinkedList()
    first = Node(10)
    second = Node(20)
    third = Node(30)
    fourth = Node(40)
    fifth = Node(50)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    llist.display()
    llist.InsertATNode(100,0)
    llist.display()

Basic Linked List Program

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

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

    def displaylist(self):
        if self.head is None:
            print('Linked List is Empty. You have to enter the data in Linked list first')
        else:
            temp = self.head
            while(temp!=None):
                print(temp.data, end="-->")
                temp = temp.next

if __name__ == "__main__":
    
    llist = LinkedList()
    llist.displaylist()
    first = Node(0)
    second = Node(10)
    third = Node(20)
    llist.head = first
    first.next = second
    second.next = third
    llist.displaylist()