C++ Program For Making Middle Node Head In A Linked List Last Updated : 23 Feb, 2023 Comments Improve Suggest changes 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. C++ // C++ program to make middle node // as head of linked list. #include <bits/stdc++.h> using namespace std; // Link list node class Node { public: int data; Node* next; }; /* Function to get the middle and set at beginning of the linked list*/ void setMiddleHead(Node** head) { if (*head == NULL) return; // To traverse list nodes one by one Node* one_node = (*head); // To traverse list nodes by skipping // one. Node* two_node = (*head); // To keep track of previous of middle Node* prev = NULL; while (two_node != NULL && two_node->next != NULL) { // For previous node of middle node prev = one_node; // Move one node each time two_node = two_node->next->next; // Move two node each time one_node = one_node->next; } // Set middle node at head prev->next = prev->next->next; one_node->next = (*head); (*head) = one_node; } // To insert a node at the beginning of // linked list. void push(Node** head_ref, int new_data) { // Allocate node Node* new_node = new Node(); new_node->data = new_data; // Link the old list of the new node new_node->next = (*head_ref); // Move the head to point to the new node (*head_ref) = new_node; } // A function to print a given linked list void printList(Node* ptr) { while (ptr != NULL) { cout << ptr->data << " "; ptr = ptr->next; } cout<<endl; } // Driver code int main() { // Create a list of 5 nodes Node* head = NULL; int i; for (i = 5; i > 0; i--) push(&head, i); cout << " list before: "; printList(head); setMiddleHead(&head); cout << " list After: "; printList(head); return 0; } // This is code is contributed by rathbhupendra Output: list before: 1 2 3 4 5 list After : 3 1 2 4 5 Time Complexity: O(n) where n is the total number of nodes in the linked list. Auxiliary Space: O(1) Please refer complete article on Make middle node head in a linked list for more details! Comment More infoAdvertise with us Next Article C++ Program For Making Middle Node Head In A Linked List K kartik Follow Improve Article Tags : Misc Linked List C++ Programs DSA Tortoise-Hare-Approach +1 More Practice Tags : Linked ListMisc Similar Reads C++ Program For Inserting A Node In A Linked List Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the listTable of ContentInsert a Node at the 9 min read C++ 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-> 5 min read C++ Program for Deleting a Node in a Linked List Write a C++ program to delete a node from the given link list.ExamplesInput: Linked List: 10 -> 20 -> 30 -> 40 -> 50, Position to delete: 3Output: 10 -> 20 -> 40 -> 50Explanation: The node at position 3 is removed. The list then connects node 20 directly to node 40.Input: Linked 6 min read C++ 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 C++ 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 Like