This is a Python program to print the middle node(s) of a linked list. The program creates a linked list using data items input from the user and prints the middle node(s) of the linked list. 1. Create a class Node with instance variables data and next. 2. Create a class LinkedList with instance variables head and last_node. 3. The variable head points to the first element in the linked list while last_node points to the last. 4. Define methods append and display inside the class LinkedList to append data and display the linked list respectively. 5. Define a function print_middle that prints the middle element(s) of the list. 6. The function print_middle iterates through the list to find its length and then iterates again to half its length. If the length of the list is even, it prints two middle elements. 7. Create an instance of LinkedList, append data to it and print its middle element(s). Here is the source code of a Python program to print the middle element(s) of a linked list. class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None self.last_node = None def append(self, data): if self.last_node is None: self.head = Node(data) self.last_node = self.head else: self.last_node.next = Node(data) self.last_node = self.last_node.next def print_middle(llist): current = llist.head length = 0 while current: current = current.next length = length + 1 current = llist.head for i in range((length - 1)//2): current = current.next if current: if length % 2 == 0: print('The two middle elements are {} and {}.' .format(current.data, current.next.data)) else: print('The middle element is {}.'.format(current.data)) else: print('The list is empty.') a_llist = LinkedList() data_list = input('Please enter the elements in the linked list: ').split() for data in data_list: a_llist.append(int(data)) print_middle(a_llist) 1. An instances of LinkedList is created. 2. The user is prompted to enter the data items for the list. 3. The function print_middle is called to print the middle element(s) of the list. Case 1: Please enter the elements in the linked list: 1 2 3 4 5 6 7 8 The two middle elements are 4 and 5. Case 2: Please enter the elements in the linked list: 5 The middle element is 5. Case 3: Please enter the elements in the linked list: 3 1 0 4 2 The middle element is 0. Sanfoundry Global Education & Learning Series – Python Programs. To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
Check this: Programming MCQs | Python Books Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.
Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Method 1: Method 2: Below image shows how printMiddle function works in the code : Python3
Output: 5->NULL The middle element is [5] 4->5->NULL The middle element is [5] 3->4->5->NULL The middle element is [4] 2->3->4->5->NULL The middle element is [4] 1->2->3->4->5->NULL The middle element is [3]Method 3:Initialize mid element as head and initialize a counter as 0. Traverse the list from head, while traversing increment the counter and change mid to mid->next whenever the counter is odd. So the mid will move only half of the total length of the list. Thanks to Narendra Kangralkar for suggesting this method. Python3
Output: 5->NULL The middle element is [5] 4->5->NULL The middle element is [5] 3->4->5->NULL The middle element is [4] 2->3->4->5->NULL The middle element is [4] 1->2->3->4->5->NULL The middle element is [3]Please refer complete article on Find the middle of a given linked list for more details! Article Tags : Linked List Python Python Programs Adobe Amazon Flipkart GE Hike Linked Lists MAQ Software Microsoft Morgan Stanley Nagarro Payu Qualcomm Samsung Veritas VMWare Wipro Zoho Practice Tags : VMWare Zoho Flipkart Morgan Stanley Amazon Microsoft Samsung Hike Payu MAQ Software Adobe Wipro Qualcomm Nagarro GE Veritas Linked List |