Make middle node head in a linked list
Last Updated :
20 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node* next;
};
void setMiddleHead(Node** head)
{
if (*head == NULL)
return ;
Node* one_node = (*head);
Node* two_node = (*head);
Node* prev = NULL;
while (two_node != NULL && two_node->next != NULL)
{
prev = one_node;
two_node = two_node->next->next;
one_node = one_node->next;
}
prev->next = prev->next->next;
one_node->next = (*head);
(*head) = one_node;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* ptr)
{
while (ptr != NULL)
{
cout << ptr->data << " " ;
ptr = ptr->next;
}
cout<<endl;
}
int main()
{
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;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void setMiddleHead( struct Node** head)
{
if (*head == NULL)
return ;
struct Node* one_node = (*head);
struct Node* two_node = (*head);
struct Node* prev = NULL;
while (two_node != NULL && two_node->next != NULL) {
prev = one_node;
two_node = two_node->next->next;
one_node = one_node->next;
}
prev->next = prev->next->next;
one_node->next = (*head);
(*head) = one_node;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node* ptr)
{
while (ptr != NULL) {
printf ( "%d " , ptr->data);
ptr = ptr->next;
}
printf ( "\n" );
}
int main()
{
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
public class GFG
{
static class Node {
int data;
Node next;
Node( int data){
this .data = data;
next = null ;
}
}
static Node head;
static void setMiddleHead()
{
if (head == null )
return ;
Node one_node = head;
Node two_node = head;
Node prev = null ;
while (two_node != null &&
two_node.next != null ) {
prev = one_node;
two_node = two_node.next.next;
one_node = one_node.next;
}
prev.next = prev.next.next;
one_node.next = head;
head = one_node;
}
static void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
static void printList(Node ptr)
{
while (ptr != null ) {
System.out.print(ptr.data+ " " );
ptr = ptr.next;
}
System.out.println();
}
public static void main(String args[])
{
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);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def setMiddleHead(head):
if (head = = None ):
return None
one_node = head
two_node = head
prev = None
while (two_node ! = None and
two_node. next ! = None ):
prev = one_node
one_node = one_node. next
two_node = two_node. next . next
prev. next = prev. next . next
one_node. next = head
head = one_node
return head
def push(head, new_data):
new_node = Node(new_data)
new_node. next = head
head = new_node
return head
def printList(head):
temp = head
while (temp! = None ):
print ( str (temp.data), end = " " )
temp = temp. next
print ("")
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)
|
C#
using System;
public class GFG
{
class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
next = null ;
}
}
static Node head;
static void setMiddleHead()
{
if (head == null )
return ;
Node one_node = head;
Node two_node = head;
Node prev = null ;
while (two_node != null &&
two_node.next != null )
{
prev = one_node;
two_node = two_node.next.next;
one_node = one_node.next;
}
prev.next = prev.next.next;
one_node.next = head;
head = one_node;
}
static void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
static void printList(Node ptr)
{
while (ptr != null ) {
Console.Write(ptr.data + " " );
ptr = ptr.next;
}
Console.WriteLine();
}
public static void Main(String []args)
{
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);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
function setMiddleHead() {
if (head == null )
return ;
one_node = head;
two_node = head;
prev = null ;
while (two_node != null && two_node.next != null ) {
prev = one_node;
two_node = two_node.next.next;
one_node = one_node.next;
}
prev.next = prev.next.next;
one_node.next = head;
head = one_node;
}
function push(new_data)
{
new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function printList( ptr) {
while (ptr != null ) {
document.write(ptr.data + " " );
ptr = ptr.next;
}
document.write( "<br/>" );
}
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);
</script>
|
Output
list before: 1 2 3 4 5
list After: 3 1 2 4 5
Time Complexity : O(n)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...