Insert in Middle of Linked List | Practice | GeeksforGeeks

Given a linked list of size N and a key. The task is to insert the key in the middle of the linked list.
Example 1:
Input:
LinkedList = 1->2->4
key = 3
Output: 1 2 3 4
Explanation: The new element is inserted
after the current middle element in the
linked list.
Example 2:
Input:
LinkedList = 10->20->40->50
key = 30
Output: 10 20 30 40 50
Explanation: The new element is inserted
after the current middle element in the
linked list and Hence, the output is
10 20 30 40 50.
 
Your Task:
The task is to complete the function insertInMiddle() which takes head reference and element to be inserted as the arguments. The printing is done automatically by the driver code.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 104
 
0
2019sushilkumarkori3 days ago
Node* insertInMiddle(Node* head, int x)
{
	// Code here
	Node* temp  = head;
	int n=0;
	while(temp!=NULL){
	    n++;
	    temp = temp->next;
	}
	temp = head;
	int i=1;
	if(n%2 == 0){
	    while(temp!=NULL){
	    if (i==n/2){
	        Node* next = temp->next;
	        temp->next = new Node(x);
	        temp->next->next = next;
	        return head;
	    }
	    temp = temp->next;
	    i++;
	    }
	}
	else{
	    while(temp!=NULL){
	    if (i==(n/2)+1){
	        Node* next = temp->next;
	        temp->next = new Node(x);
	        temp->next->next = next;
	        return head;
	    }
	    temp = temp->next;
	    i++;
	    }
	}
}
0
neelasatyasai935 days ago
#PYTHON
def insertInMid(head,node):
   #code here
   import math
   temp=head
   c=0
   while temp!=None:
       c+=1
       temp=temp.next
   if c%2==0:
       index=c/2
   else:
       index=math.ceil(c/2) 
   
   c=1
   temp=head
   while c!=index:
       c+=1
       temp=temp.next
   node.next=temp.next
   temp.next=node
+1
shahabuddinbravo402 weeks ago
Node* insertInMiddle(Node* head, int x){   Node *ptr1=head,*ptr2=head->next;      while(ptr2!=NULL && ptr2->next!=NULL ){       ptr2=ptr2->next->next;       ptr1=ptr1->next;   }      Node *new_node=new Node(x);   new_node->next=ptr1->next;      ptr1->next=new_node;         return head;         // int count=0;   // while(ptr1!=NULL){   //     ptr1=ptr1->next;   //     count++;   // }         // int index=1;   // while(true){   //     if(index==ceil((float)count/2)){   //         Node *new_node=new Node(x);   //         new_node->next=ptr2->next;              //         ptr2->next=new_node;              //         return head;   //     }   //     index++;   //     ptr2=ptr2->next;   // }     }
0
iamhunter4222 weeks ago
Node* insertInMiddle(Node* head, int x)
{
Node *t=head;
Node *data=new Node(x);
vector<Node *>a;
while(t!=NULL){
    a.push_back(t);
    t=t->next;
    
    
}
int s=a.size();
if(s%2==0){
    Node* t=a[(s/2)-1];
    data->next=t->next;
    t->next=data;
    
    
}
else{
   
    Node *t=a[(s/2)];
     data->next=t->next;
    t->next=data;
    
}
return head;



}
0
hharshit81183 weeks ago
Node* insertInMiddle(Node* head, int x){  if(!head){       return NULL;   }   Node *slow = head, *fast = head->next, *ptr = NULL;   while(fast && fast->next){       slow = slow->next;       fast = fast->next->next;   }   Node *temp = new Node(x);   temp->next = slow->next;   slow->next = temp;   return head;}
+1
kumaramresh5943 weeks ago
Node* insertInMiddle(Node* head, int x){   Node *slow=head;   Node *fast=head->next;      while(fast!=NULL && fast->next!=NULL){       slow=slow->next;       fast=fast->next->next;          }      Node *temp= new Node(x);   temp->next=slow->next;   slow->next=temp;   return head;     }
0
19bcs18694 weeks ago
public Node insertInMid(Node head, int data){       //Insert code here, return the head of modified linked list      Node temp= new Node(data);      if(head==null)      return temp;            Node slw=head,fst=head.next;      while(fst!=null && fst.next!=null)      {          slw=slw.next;          fst=fst.next.next;      }      temp.next=slw.next;      slw.next=temp;      return head;       }
 
0
bhaskarkumarpro1 month ago
Node* insertInMiddle(Node* head, int x)
{
	// Cpde here
	Node *fast=head;
	Node *slow=head;
	while(fast->next and fast->next->next){
	     slow=slow->next;
	     fast=fast->next->next;
	}
	
	Node *temp=new Node(x);
	temp->next=slow->next;
	slow->next=temp;
	return head;
}
0
siddiquerahil191 month ago
Node* insertInMiddle(Node* head, int x)
{
    Node * temp = new Node(x);
    Node * slow = head;
    Node * fast = head ->next;
    
    while(fast and fast->next){
        slow = slow->next;
        fast = fast->next->next;
        
    }
    temp->next  = slow ->next;
    slow->next= temp;
    return head;
	
	 
	  
	
}
0
subhammahanty2351 month ago
C++ Code , easy to understand ;
int len(Node * head){   //function to get the length of linked list 

    Node* temp = head;
    int c= 0;
    while(temp->next!= NULL){
        c++;
        temp = temp->next;
    }
    return c;
}
Node* insertInMiddle(Node* head, int x)
{
	int length = len(head);
	int pos = length/2;
	int i =0;
	Node* temp = head;
	while(i<pos){
	    temp= temp->next;
	    i++;
	}
	Node* new_node = new Node(x);
	new_node->next = temp->next;
	temp->next = new_node;

	return head;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.