Delete Alternate Nodes | Practice | GeeksforGeeks

Given a Singly Linked List of size N, delete all alternate nodes of the list.
Example 1:
Input:
LinkedList: 1->2->3->4->5->6
Output: 1->3->5
Explanation: Deleting alternate nodes
results in the linked list with elements
1->3->5.

 
Example 2:
Input:
LinkedList: 99->59->42->20
Output: 99->42

 
Your Task:
Your task is to complete the function deleteAlt() which takes the head of the linked list in the input parameter and modifies the given linked list accordingly.
 
Constraints:
1<=N<=100
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
0
neelasatyasai933 days ago
  #PYTHON   def deleteAlt(self, head):       temp=head       while temp!=None:         if temp.next !=None:               temp.next=temp.next.next           temp=temp.next         else:             return head       return head 
0
navins3 days ago
Python Code:
def deleteAlt(self, head):              #add code here       temp=head       count=1       while temp is not None:           if temp.next is not None:               temp.next=temp.next.next               temp=temp.next           else:               return head       return head
 
0
rohanpandey7491 month ago
Simple C++ solution: 
void deleteAlt(struct Node *head){
    // Code here
    Node *temp=head;
    while(temp && temp->next){
        temp->next=temp->next->next;
        temp=temp->next;
    }
}
0
gupta2411sumit1 month ago
// Easy Java Solution 
public void deleteAlternate (Node head){       //Write your code here              Node p = head ;              if(head == null) return ;                     while(p != null)       {                         if( p==null || p.next == null)  return ;               p.next = p.next.next ;               p = p.next ;           }                               }
0
yasirkhan2012371 month ago
void deleteAlt(struct Node *head){   // Code here   if(head == NULL||head->next == NULL){       return;   }   Node*prev = head;   while (prev!=NULL &&prev->next!=NULL ){       Node* t = prev->next;       prev->next= prev->next->next;       prev = prev->next;       delete t;   }}
0
0niharika22 months ago
while(head && head->next){
        Node *store = head->next;
        head->next = head->next->next;
        delete store;
        head = head->next;
    }
0
0niharika22 months ago
void deleteAlt(struct Node *head){
    while(head && head->next){
        head->next = head->next->next;
        head = head->next;
    }
}
0
owaischem2 months ago
PYTHON SOLUTION:
TIME TAKEN:0.0/1.0
 
class Solution:    def deleteAlt(self, head):       n=head       head=n       if n.next==None:           return head       while n!=None:           n.next=n.next.next           n=n.next           if n==None:               break           elif n.next==None:               break                  return head
+1
mayank20212 months ago
C++void deleteAlt(struct Node *head){      Node *temp=head;      while(temp)   {       if( temp->next )       {           temp->next=temp->next->next;            temp=temp->next;                  }       else         break;   }         }
0
sobhitraj1002 months ago
method 1
def deleteAlt(self, head):       # count=1       # c=1              #prev=None       #curr=head       #while curr!=None :       #     curr=curr.next       #     c+=1       # curr=head       # while count!=c:                  #     count+=1       #     if count%2==0:       #         prev=curr       #         curr=curr.next       #         if curr is not None:       #             prev.next=curr.next       #         else:       #             pass       #     else:       #         prev=curr       #         curr=curr.next                                                                     method 2       for i in range(math.floor(n/2)):                      head.next=head.next.next           head=head.next                  return
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.