Open In App
Related Articles

Java Program For Making Middle Node Head In A Linked List

Improve Article
Save Article
Like Article

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. 

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 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)
   = 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)
        // 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 &&
      != null)
            // For previous node of middle node
            prev = one_node;
            // Move one node each time
            two_node =;
            // Move two node each time
            one_node =;
        // Set middle node at head =; = 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 = 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 =;
    // Driver function
    public static void main(String args[])
        // Create a list of 5 nodes
        head = null;
        int i;
        for (i = 5; i > 0; i--)
        System.out.print(" list before: ");
        System.out.print(" list After:  ");
// This code is contributed by Sumit Ghosh


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!

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 02 Feb, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials