Delete Middle of Linked List | Practice | GeeksforGeeks

Given a singly linked list, delete middle of the linked list. For example, if given linked list is 1->2->3->4->5 then linked list should be modified to 1->2->4->5.
If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if given linked list is 1->2->3->4->5->6 then it should be modified to 1->2->3->5->6.
If the input linked list is NULL or has 1 node, then it should return NULL
Example 1:
Input:
LinkedList: 1->2->3->4->5
Output: 1 2 4 5

Example 2:
Input:
LinkedList: 2->4->6->7->5->1
Output: 2 4 6 5 1
Your Task:
The task is to complete the function deleteMid() which should delete the middle element from the linked list and return the head of the modified linked list. If the linked list is empty then it should return NULL.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 1000
1 <= value <= 1000
0
prabhakarsati294261 week ago
def deleteMid(head):   '''   head:  head of given linkedList   return: head of resultant llist   '''      #code here   slow,fast = head,head   pre = None   while fast and fast.next:              pre = slow       slow = slow.next       fast = fast.next.next            pre.next = pre.next.next   return head 
0
csedeepak2 weeks ago
Node* deleteMid(Node* head){   // Your Code Here   int cnt=0;   Node *curr=head;   if(head==NULL || head->next==NULL){       free(head);       return NULL;   }   while(curr){       cnt++;       curr=curr->next;   }        int pos=cnt/2 + 1;        Node *temp=head;        Node *back;        for(int i=1; i<=pos-2 && temp!=NULL ; i++){             temp=temp->next;        }        back=temp->next;        temp->next=temp->next->next;        free(back);        return head;   }
0
harshscode2 weeks ago
 Node *slow=head;   Node *fast=head;      Node *prev=NULL;      while(fast and fast->next)   {       prev=slow;       slow=slow->next;       fast=fast->next->next;   }   prev->next=prev->next->next; 
   return head;
0
ns2847192 weeks ago
Node* deleteMid(Node* head){
        Node* prev=NULL;   Node* slow=head;   Node* fast=head;   while(fast && fast->next!=NULL)   {       fast=fast->next->next;       prev=slow;       slow=slow->next;   }   prev->next=prev->next->next;   return head;}
0
hharshit81183 weeks ago
Node* deleteMid(Node* head){   if(!head || !head->next){       return NULL;   }   Node *slow = head, *ptr = NULL, *fast = head;   while(fast!=NULL && fast->next != NULL){       ptr =slow;       slow = slow->next;       fast = fast->next->next;   }       Node *temp = ptr->next;       ptr->next = temp->next;       delete(temp);   return head;}
0
goelyash3 weeks ago
Node* deleteMid(Node* head)
{
    if(!head)return NULL;
    Node* slow=head,*fast=head,*prev=head;
    while(fast && fast->next){
        
        slow= slow->next;
        fast=fast->next->next;
    }
    while(prev->next!=slow)prev=prev->next;
    prev->next=prev->next->next;
    slow->next=NULL;
    return head;
}
0
user_8m8m3 weeks ago
 Node deleteMid(Node head) {       // This is method only submission.       // You only need to complete the method.       if(head.next.next==null)       {           head.next=null;           return head;       }       Node slow = head;       Node fast = head;       while(fast.next!=null)       {           fast = fast.next;           if(fast.next!=null)           {               fast=fast.next;           }           slow = slow.next;       }       slow.data = slow.next.data;       slow.next=slow.next.next;       return head;   }
0
sangrambachu4 weeks ago
class Solution {
    Node deleteMid(Node head) {
        // This is method only submission.
        // You only need to complete the method.
        
        // Find mid
        int index = 0;
        Node fast = head;
        Node slow = head;
        
        while(fast != null && fast.next != null) {
            index++;
            slow = slow.next;
            fast = fast.next.next;
        }
        
        Node prev = head;
        for(int i=0; i<index-1; i++) {
            prev = prev.next;
        }
        
        prev.next = prev.next.next;
        return head;
    }
}
0
sanusonkar11 month ago
Java Solution
Time complexity =O(n)
 Node deleteMid(Node head) {
       Node prev=null;
       Node fast=head;
       Node slow=head;
       //first try to find 1 node before mid
       //ex-2 4 5 6 7 then we get in prev=>4 value node 
       while(fast!=null && fast.next!=null){
           fast=fast.next.next;
           prev=slow;
           slow=slow.next;
       }
       prev.next=prev.next.next;
       return head;
   }
0
ajinkyahimanshu0071 month ago
Using normal basics:
 
1.
def deleteMid(head):   '''   head:  head of given linkedList   return: head of resultant llist   '''   n = length(head)   return delNode(head,n//2)   def length(head):   count = 0   curr = head   while curr:       count += 1       curr = curr.next   return count   def delNode(head,k):   temp = 0   curr = head   while temp<k-1:       curr = curr.next       temp += 1   curr.next = curr.next.next      return head
 
2.
if not head:       return None   dummy = slow = Node(data = None)   dummy.next = head   fast = head      while fast and fast.next:       slow = slow.next       fast = fast.next.next   slow.next = slow.next.next      return dummy.next
 
the 1st one is faster is than the 2nd one
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.