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