Segregate even and odd nodes in a Link List | Practice | GeeksforGeeks

Given a link list of size N, modify the list such that all the even numbers appear before all the odd numbers in the modified list. The order of appearance of numbers within each segregation should be same as that in the original list.
NOTE: Don't create a new linked list, instead rearrange the provided one.

Example 1:
Input: 
N = 7
Link List:
17 -> 15 -> 8 -> 9 -> 2 -> 4 -> 6 -> NULL

Output: 8 2 4 6 17 15 9

Explaination: 8,2,4,6 are the even numbers 
so they appear first and 17,15,9 are odd 
numbers that appear later.

Example 2:
Input:
N = 4
Link List:
1 -> 3 -> 5 -> 7

Output: 1 3 5 7

Explaination: There is no even number. 
So ne need for modification.

Your Task:
You do not need to read input or print anything. Your task is to complete the function divide() which takes N and head of Link List as input parameters and returns the head of modified link list. Don't create a new linked list, instead rearrange the provided one.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 105
1 ≤ Each element of the list ≤ 105
0
taiphanvan2403
This comment was deleted.
0
swapniltayal4226 days ago
class Solution{public:   Node* divide(int N, Node *head){       // code here       Node* even = new Node(-1);       Node* evenS = even;       Node* odd = new Node(-1);       Node* oddS = odd;       Node* temp = head;              while(temp != NULL){           int s = temp->data;           Node* temp2 = new Node(s);                      if(s % 2 == 0){               even->next = temp2;               even = even->next;           }else{               odd->next = temp2;               odd = odd->next;           }           temp = temp->next;       }       even->next = oddS->next;       return evenS->next;          }};
+1
09himanshusah1 week ago
JavaScript Solution
class Solution {
  divide(N,head){
    //code here
    let dummyOdd = new Node(-1);
    let dummyEven = new Node(-2);
    let oddTail = dummyOdd;
    let evenTail = dummyEven;
    let cur = head;
    while( cur !== null) {
        if(cur.data % 2 !== 0) {
            oddTail.next = cur;
            oddTail = oddTail.next;
        } else {
            evenTail.next = cur;
            evenTail = evenTail.next;
        }
        cur = cur.next;
    }
    evenTail.next = dummyOdd.next;
    oddTail.next = null;
    return dummyEven.next;
  }
}

0
siyalishiya1 week ago
class Solution{      Node reverse(Node head) {       Node prev=null,cur=head,nxt=cur.next;       while(cur.next!=null) {           cur.next=prev;           prev=cur;           cur=nxt;           nxt=nxt.next;       }       cur.next=prev;       return cur;          }      Node divide(int N, Node head){       // code here       Node val = head;       Node odd=null,even=null;              while(head!=null) {           if(val.data%2!=0) {               head=head.next;               val.next=odd;               odd=val;               val=head;           }           else {               head=head.next;               val.next=even;               even=val;               val=head;           }       }              even = reverse(even);       odd = reverse(odd);              head=even;              val=head;              while(val.next!=null) {           val=val.next;       }              val.next=odd;              return head;          }}
 
This gives right solution. why is it wrong?
0
yadavpradeepji202 weeks ago
    Node* divide(int N, Node *head){    Node * p=head;    vector<int>v,w;        while(p)    {        v.push_back(p->data);        p=p->next;    }    p=head;    for(int i=0;i<v.size();i++)    {        if(v[i]%2==0)        w.push_back(v[i]);    }     for(int i=0;i<v.size();i++)    {        if(v[i]%2!=0)        w.push_back(v[i]);    }    int i=0;    while(p)    {        p->data=w[i++];       p=p->next;            }    return head;   }}; 
0
bajpayeevinayak2283 weeks ago
// { Driver Code Starts//Initial template for C++
#include <bits/stdc++.h>using namespace std;
struct Node{   int data;   struct Node* next;      Node(int x){       data = x;       next = NULL;   }};void printList(Node* node) {    while (node != NULL) {        cout << node->data <<" ";        node = node->next;    }     cout<<"\n";}
// } Driver Code Ends//User function template for C++
/*struct Node{   int data;   struct Node* next;      Node(int x){       data = x;       next = NULL;   }};*/class Solution{public:   Node* divide(int N, Node *head){       // code here        vector<int>l1;        vector<int>l2;        Node *cur=head;        while(cur!=NULL)        {            if(cur->data %2!=0)            {                l2.push_back(cur->data);            }            else{                l1.push_back(cur->data);            }            cur=cur->next;        }        Node *b=head;        for(auto it:l1)        {            b->data=it;            b=b->next;        }        for(auto it:l2)        {            b->data=it;            b=b->next;        }        return head;   }};
// { Driver Code Starts.
int main() {   //code   int t;   cin>>t;   while(t--){       int N;       cin>>N;       int data;       cin>>data;       struct Node *head = new Node(data);       struct Node *tail = head;       for (int i = 0; i < N-1; ++i)       {           cin>>data;           tail->next = new Node(data);           tail = tail->next;       }              Solution ob;       Node *ans = ob.divide(N, head);       printList(ans);    }   return 0;} // } Driver Code Ends
0
shahabuddinbravo403 weeks ago
class Solution{public:   Node* divide(int N, Node *head){              Node *even=new Node(-1),
*odd=new Node(-1),*e=even,
*o=odd,*ptr=head;              while(ptr)       {           if(ptr->data%2==0){               even->next=ptr;               even=even->next;           }           else{               odd->next=ptr;               odd=odd->next;           }                      ptr=ptr->next;       }              odd->next=NULL;              e=e->next;       o=o->next;              if(e!=NULL){           even->next=o;           head=e;       }       else{           head=o;       }          }};
0
tthakare733 weeks ago
//java Solution -> TC -> 0.94
//hint -> Use ArrayList to store even and odds

class Solution{
    
    Node divide(int N, Node head){
        ArrayList<Integer> odd = new ArrayList<>();
        ArrayList<Integer> even = new ArrayList<>();
        while(head != null){
            if(head.data % 2 == 0){
                even.add(head.data);
            } else {
                odd.add(head.data);
            }
            head = head.next;
        }
        Node Result = null, dummy = null;
        for(int i = 0; i < even.size(); i++){
            Node newNode = new Node(even.get(i));
            if(Result == null){
                Result = newNode;
                dummy = Result;
            } else {
                dummy.next = newNode;
                dummy = dummy.next;
            }
        }
        
        for(int i = 0; i < odd.size(); i++){
            Node newNode = new Node(odd.get(i));
            if(Result == null){
                Result = newNode;
                dummy = Result;
            } else {
                dummy.next = newNode;
                dummy = dummy.next;
            }
        }
        return Result;
    }
} 
0
kimbbang4 weeks ago
Segregate even and odd nodes in a Linked List
https://www.geeksforgeeks.org/segregate-even-and-odd-elements-in-a-linked-list/ 
0
harshilgupta00991 month ago
Node* divide(int N, Node *head){
        // code here
        Node* evHead=NULL;
        Node* odHead=NULL;
        
        Node * ev;
        Node * od;
        
        while(head!=NULL){
            if((head->data)%2==0){
                if(evHead==NULL){
                    evHead=head;
                    ev=head;
                }
                else{
                    ev->next=head;
                    ev=ev->next;
                }
            }else{
                if(odHead==NULL){
                    odHead=head;
                    od=head;
                }
                else{
                    od->next=head;
                    od=od->next;
                }
            }
            head=head->next;
        }
        
        if(evHead==NULL)
            return odHead;
        if(od!=NULL)
            od->next=NULL;
        
        ev->next=odHead;
        return evHead;
    }
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.