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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

/* Function to get the middle and set at
{
return;

// To traverse list nodes one by one

// To traverse list nodes by skipping
// one.

// To keep track of previous of middle
struct 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;
}

// To insert a node at the beginning of linked
// list.
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));

new_node->data = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */
}

// A  function to print a given linked list
void printList(struct Node* ptr)
{
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

/* Driver function*/
int main()
{
// Create a list of 5 nodes
int i;
for (i = 5; i > 0; i--)

printf(" list before: ");

printf(" list After:  ");

return 0;
}
```

## Java

```// Java program to make middle node
public class GFG
{
static class Node {
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}

/* Function to get the middle and
set at beginning of the linked list*/
{
return;

// To traverse list nodes one
// by one

// To traverse list nodes by
// skipping one.

// 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;
}

// To insert a node at the beginning of
static void push(int new_data)
{
/* allocate node */
Node new_node = new Node(new_data);

/* link the old list off the new node */

/* move the head to point to the 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
int i;
for (i = 5; i > 0; i--)
push(i);

System.out.print(" list before: ");

System.out.print(" list After:  ");

}
}
// This code is contributed by Sumit Ghosh
```

Output:

```list before: 1 2 3 4 5
list After : 3 1 2 4 5
```

This article is contributed by R_Raj. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.5 Average Difficulty : 1.5/5.0
Based on 4 vote(s)