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.
Implementation:
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 |
C
// C program to make middle node as head of // linked list. #include <stdio.h> #include <stdlib.h> /* Link list node */ struct Node {
int data;
struct Node* next;
}; /* Function to get the middle and set at beginning of the linked list*/
void setMiddleHead( struct Node** head)
{ if (*head == NULL)
return ;
// To traverse list nodes one by one
struct Node* one_node = (*head);
// To traverse list nodes by skipping
// one.
struct Node* two_node = (*head);
// 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;
one_node->next = (*head);
(*head) = one_node;
} // 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 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( 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
struct Node* head = NULL;
int i;
for (i = 5; i > 0; i--)
push(&head, i);
printf ( " list before: " );
printList(head);
setMiddleHead(&head);
printf ( " list After: " );
printList(head);
return 0;
} |
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 |
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 |
C#
// C# program to make middle node // as head of Linked list using System;
public class GFG
{ /* Link list node */
class Node
{
public int data;
public Node next;
public 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 ) {
Console.Write(ptr.data + " " );
ptr = ptr.next;
}
Console.WriteLine();
}
/* Driver code*/
public static void Main(String []args)
{
// Create a list of 5 nodes
head = null ;
int i;
for (i = 5; i > 0; i--)
push(i);
Console.Write( " list before: " );
printList(head);
setMiddleHead();
Console.Write( " list After: " );
printList(head);
}
} // This code is contributed by Rajput-Ji |
Javascript
<script> // javascript program to make middle node // as head of Linked list /* Link list node */
class Node { constructor(val) {
this .data = val;
this .next = null ;
}
} var head;
/*
* Function to get the middle and set at beginning of the linked list
*/
function setMiddleHead() {
if (head == null )
return ;
// To traverse list nodes one
// by one
one_node = head;
// To traverse list nodes by
// skipping one.
two_node = head;
// To keep track of previous of middle
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.
function push(new_data)
{
/* allocate 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
function printList( ptr) {
while (ptr != null ) {
document.write(ptr.data + " " );
ptr = ptr.next;
}
document.write( "<br/>" );
}
/* Driver function */
// Create a list of 5 nodes
head = null ;
var i;
for (i = 5; i > 0; i--)
push(i);
document.write( " list before: " );
printList(head);
setMiddleHead();
document.write( " list After: " );
printList(head);
// This code is contributed by aashish1995 </script> |
Output
list before: 1 2 3 4 5 list After: 3 1 2 4 5
Time Complexity : O(n)
Auxiliary Space : O(1)