Delete middle of linked listGiven a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 Show If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if given linked list is 1->2->3->4->5->6 then it should be modified to 1->2->3->5->6. If the input linked list has 1 node, then this node should be deleted and a new head should be returned. Python Program To Delete Middle Of Linked ListGiven a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if given linked list is 1->2->3->4->5->6 then it should be modified to 1->2->3->5->6. If the input linked list has 1 node, then this node should be deleted and a new head should be returned. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Simple solution: The idea is to first count the number of nodes in a linked list, then delete n/2’th node using the simple deletion process. Python3
Output: Given Linked List 1->2->3->4->NULL Linked List after deletion of middle 1->2->4->NULL Complexity Analysis:
Efficient solution: Below is the implementation. Python3
Output: Complexity Analysis:
Please refer complete article on Delete middle of linked list for more details!
Article Tags :
Linked List Python Programs
Flipkart Microsoft Tortoise-Hare-Approach Practice Tags :
Flipkart Microsoft Linked List 6. Python program to delete a node from the middle of the Circular Linked ListIn this program, we will create a circular linked list and delete a node from the middle of the list. If the list is empty, display the message "List is empty". If the list is not empty, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list. We maintain two pointers temp and current. Current will point to the previous node of temp. We will iterate through the list until mid-point is reached then the current will point to the middle node. We delete middle node such that current's next will be temp's next node. Circular linked list after deleting the node from the middle of the list Consider the above list. Size of the list is 4. Mid-point of the node is 2. To remove C from the list, we will iterate through the list till mid-point. Now current will point to B and temp will point to C. C will be removed when B will point to D. ALGORITHM:
a. display() will show all the nodes present in the list.
PROGRAM:Output: Original List:
1 2 3 4
Updated List:
1 3 4
Updated List:
1 4
Updated List:
4
Updated List:
List is empty
About Linked ListLinked List is a one of the DataStructure used for storing collection of data. A Linked List has the following properties:
Basic Structure of Linked List- Delete middle node from linked list in pythonPython program for Delete middle node from linked list. Here problem description and explanation. # Python 3 program for # Delete middle node of linked list # Linked list node class LinkNode : def __init__(self, data) : self.data = data self.next = None class SingleLL : def __init__(self) : self.head = None # Adding new node at beginning of linked list def addNode(self, data) : # Create new node node = LinkNode(data) # Connect current node to previous head node.next = self.head self.head = node # Display linked list element def display(self) : if (self.head == None) : return temp = self.head # iterating linked list elements while (temp != None) : # Display node value print(temp.data, end = " → ") # Visit to next node temp = temp.next print(" NULL") # Delete the middle node of linked list def deleteMid(self) : if (self.head == None) : # When linked list are no elements print("Empty Linked List") elif (self.head.next == None and self.head.next.next == None) : # When linked list are less than of 3 nodes print("Less then 3 node of linked list") else : temp = self.head midPrevious = None # Find the middle and its previous node while (temp != None and temp.next != None and temp.next.next != None) : if (midPrevious == None) : # When first node midPrevious = temp else : # Visit to next node midPrevious = midPrevious.next # Visit to next second next node temp = temp.next.next # Get the node which is deleting temp = midPrevious.next # Show deleted node print("Delete node : ", temp.data) # Change link midPrevious.next = temp.next def main() : sll = SingleLL() # Linked list # 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL sll.addNode(1) sll.addNode(2) sll.addNode(3) sll.addNode(4) sll.addNode(5) sll.addNode(6) sll.addNode(7) print("Before Delete middle node") sll.display() # Delete middle node sll.deleteMid() print("After Delete middle node") sll.display() if __name__ == "__main__": main()Output Before Delete middle node 7 → 6 → 5 → 4 → 3 → 2 → 1 → NULL Delete node : 4 After Delete middle node 7 → 6 → 5 → 3 → 2 → 1 → NULL |