Program to remove duplicate element from a sorted Linked List

 In this program we will see that how can we remove duplicate element from Linked List after sorting it









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 sort(self):
        temp = self.head
        count = 0
        while count<self.length():
            temp1 = temp
            while temp1.next!=None:
                if temp1.next.data<temp1.data:
                    hold = temp1.next.data
                    temp1.next.data = temp1.data
                    temp1.data = hold
                    temp1 = temp1.next
                else:
                    temp1 = temp1.next
            count = count+1

    def removeDuplicate(self):
        temp = self.head
        while temp!=None:
            temp1=temp
            while temp1.next!=None:
                dup = temp1.next
                if temp.data==temp1.next.data:
                    dup = temp1.next.next
                    temp1.next = dup
                    del  dup
                else:
                    temp1 = temp1.next
            temp = temp.next

if __name__ == "__main__":
    llist = LinkedList()
    first = Node(50)
    second = Node(10)
    third = Node(20)
    fourth = Node(10)
    fifth = Node(10)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    llist.display()
    llist.sort()
    llist.removeDuplicate()
    print("\nAfter sorting and removing duplicates")
    llist.display()


Program to remove duplicates from an unsorted Linked List

 In this program we will see that how can we remove duplicate element from Linked List without sorting it








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 indexData(self,index):
        temp = self.head
        count =1
        while count!=index:
            temp = temp.next
            count = count+1
        return temp.data
    def removeDuplicate(self):
        temp = self.head
        while temp!=None:
            temp1=temp
            while temp1.next!=None:
                dup = temp1.next
                if temp.data==temp1.next.data:
                    dup = temp1.next.next
                    temp1.next = dup
                    del  dup
                else:
                    temp1 = temp1.next
            temp = temp.next


if __name__=="__main__":
    llist = LinkedList()
    first = Node(10)
    second = Node(20)
    third = Node(30)
    fourth = Node(20)
    fifth = Node(50)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    llist.display()
    llist.removeDuplicate()
    print("\nAfter removing duplicates")
    llist.display()
    
    



Program to bubble sort LinkedList

 In this program we will see the bubble sort algorithm to sort 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 length(self):
        temp = self.head
        count = 0
        while temp!=None:
            count = count+1
            temp = temp.next
        return count
    def sort(self):
        temp = self.head
        count = 0
        while count<self.length():
            temp1 = temp
            while temp1.next!=None:
                if temp1.next.data<temp1.data:
                    hold = temp1.next.data
                    temp1.next.data = temp1.data
                    temp1.data = hold
                    temp1 = temp1.next
                else:
                    temp1 = temp1.next
            count = count+1


if __name__ == "__main__":
    llist = LinkedList()
    first = Node(50)
    second = Node(40)
    third = Node(30)
    fourth = Node(30)
    fifth = Node(10)
    llist.head = first
    first.next = second
    second.next = third
    third.next = fourth
    fourth.next = fifth
    llist.display()
    llist.sort()
    print("\nAfter sorting")
    llist.display()

Program to reverse a Linked List

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







input: 10=>20=>30=>40=>50=>
output: 50=>40=>30=>20=>10=>



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 indexData(self,index):
        temp = self.head
        count =1
        while count!=index:
            temp = temp.next
            count = count+1
        return temp.data
    def reverse(self):
        count = self.length()
        while count>0:
            print(self.indexData(count),end="=>")
            count = count-1


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()
    print("\nAfter reverese")
    llist.reverse()
    
    



Swap two data in Linked List without swapping node

 In this program we will see how to swap two node data without swapping the actual node.


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 swap(self,index1,index2):
        if index2>self.length() or index2<1 or index1>self.length() or index1<1:
            print("\nEnter a valid index")
        else: 
            temp1 = self.getAddress(index1)
            temp2 = self.getAddress(index2)
            temp_data = 0
            print(f"\nvalue at {temp1} before swap is {temp1.data}")
            temp_data = temp1.data
            temp1.data = temp2.data
            temp2.data = temp_data
            print(f"\nvalue at {temp1} after swap is {temp1.data}")

    def getAddress(self,index):
        temp = self.head
        count = 1
        while count!=index:
            temp = temp.next
            count = count+1
        return temp
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.swap(2,3)
    print("\nAfter Swap")
    llist.display()
    

Program to print node data at given index

 In this program we will see how we can print the node data at given index


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 deleteAtGivenIndex(self,index):
        pos=1
        temp = self.head
        if index>self.length() or index<1:
            print("\nGiven Index is not valid!!")
        elif pos == index:
            self.head = self.head.next
        else:
            while temp!=None:
                pos = pos+1
                if pos==index:
                    temp.next = temp.next.next
                    
                else:
                    temp = temp.next
                    

    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.deleteAtGivenIndex(2)
    print('\nAfter delete')
    llist.display()

Program to delete a given data in linked list

 In this program we will see how we can delete a given data present in 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
        print('\n')
    def deleteGivenElement(self,data):
        temp = self.head
        temp1 = self.head
        flag=0
        while temp1!=None:
            if temp1.data == data:
                flag=1
                break
            temp1=temp1.next
        if flag==1:
            if self.head.data==data:
                self.head=self.head.next
            else:
                while temp!=None:
                    if temp.next.data == data:
                        temp.next = temp.next.next
                        flag=1
                        break
                    else:
                        temp = temp.next
        else:
            print('data is not present')
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.deleteGivenElement(50)
    llist.display()

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