Python Program For Making Middle Node Head In A Linked List Last Updated : 19 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5 Input: 1 2 3 4 5 6 Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves one at a time and second one moves two at a time. When second pointer reaches end, first reaches middle. We also keep track of previous of first pointer so that we can remove middle node from its current position and can make it head. Python3 # Python3 program to make middle node # as head of Linked list # Linked List node class Node: def __init__(self, data): self.data = data self.next = None # function to get the middle node # set it as the beginning of the # linked list def setMiddleHead(head): if(head == None): return None # To traverse nodes # one by one one_node = head # To traverse nodes by # skipping one two_node = head # To keep track of previous middle prev = None while(two_node != None and two_node.next != None): # For previous node of middle node prev = one_node # Move one node each time one_node = one_node.next # Move two nodes each time two_node = two_node.next.next # Set middle node at head prev.next = prev.next.next one_node.next = head head = one_node # Return the modified head return head def push(head, new_data): # Allocate new node new_node = Node(new_data) #Link the old list to new node new_node.next = head # Move the head to point the new node head = new_node # Return the modified head return head # A function to print a given linked list def printList(head): temp = head while (temp!=None): print(str(temp.data), end = " ") temp = temp.next print("") # Create a list of 5 nodes head = None for i in range(5, 0, -1): head = push(head, i) print(" list before: ", end = "") printList(head) head = setMiddleHead(head) print(" list After: ", end = "") printList(head) # This code is contributed by Pranav Devarakonda Output: list before: 1 2 3 4 5 list After : 3 1 2 4 5 Time complexity: O(n) where n is the size of the linked list Space Complexity: O(1) since using constant space Please refer complete article on Make middle node head in a linked list for more details! Comment More infoAdvertise with us Next Article Python Program for Deleting a Node in a Linked List K kartik Follow Improve Article Tags : Misc Linked List Python Programs DSA Tortoise-Hare-Approach Python-DSA +2 More Practice Tags : Linked ListMisc Similar Reads Python Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2-> 4 min read Python Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of linked list. Python # Node class class Node: # Function to initialize the # node o 7 min read Python Program for Deleting a Node in a Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Python Program To Delete N Nodes After M Nodes Of A Linked List Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.Difficulty Level: Rookie Examples: Input: M = 2, N = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked L 3 min read Python Program To Delete Middle Of Linked List Given 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 g 4 min read Python Program For Finding The Middle Element Of A Given Linked List 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. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if given linked list 4 min read Like