Java Program For Making Middle Node Head In A Linked List Last Updated : 02 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. Java // Java program to make middle node // as head of Linked list public class GFG { // Link list node static class Node { int data; Node next; Node(int data) { this.data = data; next = null; } } static Node head; /* Function to get the middle and set at beginning of the linked list*/ static void setMiddleHead() { 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. static void push(int new_data) { // Allocate node Node new_node = new Node(new_data); // Link the old list of the new node new_node.next = head; // Move the head to point to the // new node head = new_node; } // A function to print a given linked list static void printList(Node ptr) { while (ptr != null) { System.out.print(ptr.data + " "); ptr = ptr.next; } System.out.println(); } // Driver function public static void main(String args[]) { // Create a list of 5 nodes head = null; int i; for (i = 5; i > 0; i--) push(i); System.out.print(" list before: "); printList(head); setMiddleHead(); System.out.print(" list After: "); printList(head); } } // This code is contributed by Sumit Ghosh 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. 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 Java Program For Making Middle Node Head In A Linked List kartik Follow Improve Article Tags : Misc Linked List Java Programs DSA Tortoise-Hare-Approach +1 More Practice Tags : Linked ListMisc Similar Reads Java 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 Java 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 the linked list. Java // Linked List Class class LinkedList { // Head of list Node 7 min read Java 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 Java Program For Finding The Middle Element Of A Given Linked List Given a Singly linked list, find the middle of the linked list. If there are even nodes, then there would be two middle nodes, we need to print the second middle element. Example of Finding Middle Element of Linked ListInput: 1->2->3->4->5 Output: 3 Input: 1->2->3->4->5-> 6 min read Java Program For Flattening A Multilevel Linked List Given a linked list where in addition to the next pointer, each node has a child pointer, which may or may not point to a separate list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the below figure. You are given the he 5 min read Java Program to Insert a New Node at the Middle of the Circular Linked List Given a Circular Linked List, the task is to add a New Node at the Middle of the List. Let's consider the following Circular Linked List: List-Before-InsertionList-After-InsertionCreate a new node (New_node).Check for an empty list. If the list is empty then insert the node as head.For non-empty lis 3 min read Java Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list 4 min read Java Program For Rearranging A Given Linked List In-Place. Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 - 8 min read Java Program to Delete a Node From the Middle of the Circular Linked List In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list. Algorithm C 4 min read Java Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t 3 min read Like