Insert the new node to the linked list at the beginning: ======================================================== # Printing of linkedlist data # Creation of linkedlist class Node: # general structure of node in a linkedlist def __init__(self,data = None): self.data = data self.next = None class singleLinkedList: def __init__(self): self.head = None def printLinkedList(self): value = self.head # Traversing on the linkedlist while value is not None: print(value.data) value = value.next # adding new node at the beginning of linkedlist def atBegin(self,ndata): nnode = Node(ndata) nnode.next = self.head self.head = nnode l1 = singleLinkedList() l1.head = Node("Monday") l2 = Node("Tuesday") l3 = Node("Wednesday") l4 = Node("Thursday") l5 = Node("Friday") l6 = Node("Saturday") l7 = Node("Sunday") l1.head.next = l2 l2.next = l3 l3.next = l4 l4.next = l5 l5.next = l6 l6.next = l7 # l1.printLinkedList() l1.atBegin("Apple") l1.printLinkedList() =========================================================== How to insert the new node at the end of the linked list ======================================================== # Printing of linkedlist data # Creation of linkedlist class Node: # general structure of node in a linkedlist def __init__(self,data = None): self.data = data self.next = None class singleLinkedList: def __init__(self): self.head = None def printLinkedList(self): value = self.head # Traversing on the linkedlist while value is not None: print(value.data) value = value.next # adding new node at the beginning of linkedlist def atBegin(self,ndata): nnode = Node(ndata) nnode.next = self.head self.head = nnode # adding new node at the end of linkedlist def atEnd(self,ndata): nnode = Node(ndata) if self.head is None: self.head = nnode return last = self.head while last.next: last = last.next last.next = nnode l1 = singleLinkedList() l1.head = Node("Monday") l2 = Node("Tuesday") l3 = Node("Wednesday") l4 = Node("Thursday") l5 = Node("Friday") l6 = Node("Saturday") l7 = Node("Sunday") l1.head.next = l2 l2.next = l3 l3.next = l4 l4.next = l5 l5.next = l6 l6.next = l7 # l1.printLinkedList() l1.atBegin("Apple") l1.atEnd("Mango") l1.printLinkedList() ======================================================= How to insert the node between two nodes: ========================================= # Printing of linkedlist data # Creation of linkedlist class Node: # general structure of node in a linkedlist def __init__(self,data = None): self.data = data self.next = None class singleLinkedList: def __init__(self): self.head = None def printLinkedList(self): value = self.head # Traversing on the linkedlist while value is not None: print(value.data) value = value.next # adding new node at the beginning of linkedlist def atBegin(self,ndata): nnode = Node(ndata) nnode.next = self.head self.head = nnode # adding new node at the end of linkedlist def atEnd(self,ndata): nnode = Node(ndata) if self.head is None: self.head = nnode return last = self.head while last.next: last = last.next last.next = nnode # Adding new node between two nodes def inBetween(self,m_node,ndata): if m_node is None: print("The Mentioned node is not in a linkedlist.") return nnode = Node(ndata) nnode.next = m_node.next m_node.next = nnode l1 = singleLinkedList() l1.head = Node("Monday") l2 = Node("Tuesday") l3 = Node("Wednesday") l4 = Node("Thursday") l5 = Node("Friday") l6 = Node("Saturday") l7 = Node("Sunday") l1.head.next = l2 l2.next = l3 l3.next = l4 l4.next = l5 l5.next = l6 l6.next = l7 # l1.printLinkedList() l1.atBegin("Apple") l1.atEnd("Mango") l1.inBetween(l6.next,"Python") l1.printLinkedList() ============================================== Removing of node from linked list: ================================== # Printing of linkedlist data # Creation of linkedlist class Node: # general structure of node in a linkedlist def __init__(self,data = None): self.data = data self.next = None class singleLinkedList: def __init__(self): self.head = None def printLinkedList(self): value = self.head # Traversing on the linkedlist while value is not None: print(value.data) value = value.next # adding new node at the beginning of linkedlist def atBegin(self,ndata): nnode = Node(ndata) nnode.next = self.head self.head = nnode # adding new node at the end of linkedlist def atEnd(self,ndata): nnode = Node(ndata) if self.head is None: self.head = nnode return last = self.head while last.next: last = last.next last.next = nnode # Adding new node between two nodes def inBetween(self,m_node,ndata): if m_node is None: print("The Mentioned node is not in a linkedlist.") return nnode = Node(ndata) nnode.next = m_node.next m_node.next = nnode # remove the node def removeNode(self,key): h = self.head if h is not None: if h.data == key: self.head = h.next h = None return while h is not None: if h.data == key: break preview = h h = h.next if h == None: return preview.next = h.next h = None l1 = singleLinkedList() l1.head = Node("Monday") l2 = Node("Tuesday") l3 = Node("Wednesday") l4 = Node("Thursday") l5 = Node("Friday") l6 = Node("Saturday") l7 = Node("Sunday") l1.head.next = l2 l2.next = l3 l3.next = l4 l4.next = l5 l5.next = l6 l6.next = l7 # l1.printLinkedList() l1.atBegin("Apple") l1.atEnd("Mango") l1.inBetween(l6.next,"Python") l1.removeNode("Friday") l1.printLinkedList() ============================================ # WAP TO SORT THE COLLECTION/DATA STRUCTURE WITH BUILT-IN METHODS # [1,0,7,9,2,5] # [0,1,7,9,2,5] # [0,1,7,2,9,5] # [0,1,7,2,5,9] # [0,1,2,7,5,9] # [0,1,2,5,7,9] def Sorting(ld): for i in range(len(ld)-1,0,-1):# 5 2 9 7 0 1 for j in range(i): # 0 to 4 if ld[j] > ld[j+1]: temp = ld[j] ld[j] = ld[j+1] ld[j+1] = temp ld = [19,2,31,45,6,11,121,27] print(ld) Sorting(ld) print(ld)