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 Making Middle Node Head 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 Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Like