Rearrange a linked list | Practice | GeeksforGeeks

Given a singly linked list, the task is to rearrange it in a way that all odd position nodes are together and all even positions node are together.
Assume the first element to be at position 1 followed by second element at position 2 and so on.
Note: You should place all odd positioned nodes first and then the even positioned ones. (considering 1 based indexing). Also, the relative order of odd positioned nodes and even positioned nodes should be maintained.
Example 1:
Input:
LinkedList: 1->2->3->4
Output: 1 3 2 4 
Explanation: 
Odd elements are 1, 3 and even elements are 
2, 4. Hence, resultant linked list is 
1->3->2->4.

Example 2:
Input:
LinkedList: 1->2->3->4->5
Output: 1 3 5 2 4 
Explanation: 
Odd elements are 1, 3, 5 and even elements are
2, 4. Hence, resultant linked list is
â€‹1->3->5->2->4.

Your Task:
The task is to complete the function rearrangeEvenOdd() which rearranges the nodes in the linked list as required and doesn't return anything.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ Size of the linked list ≤ 104
0 ≤ value of linked list ≤ 103
0
swapniltayal4225 days ago
class Solution{   public:   void rearrangeEvenOdd(Node *head)   {      // Your Code here      if (head == NULL || head->next == NULL){          return;      }            Node* odd = head;      Node* even = head->next;      Node* evenStart = even;      Node* oddStart = odd;            while (odd->next != NULL && even->next != NULL){          odd->next = even->next;          odd = odd->next;          even->next = odd->next;          even = even->next;      }            odd->next = evenStart;      head = oddStart;   }};
0
shakshamkaushik15 days ago
Time --2.25
   void rearrangeEvenOdd(Node head)
   {
       Node curr = head;
       Node q = new Node(0);
       Node ans = q;
       int count = 1;
       while(curr!=null){
           if(count%2==1){
               q.next = new Node(curr.data);
               q = q.next;
           }
           count++;
           curr = curr.next;
       }        curr = head;       int count1 = 1;       while (curr!=null){           if(count1%2==0){               q.next = new Node(curr.data);               q = q.next;           }           count1++;           curr= curr.next;       }       Node temp = ans.next;       while(temp!=null){           head.data = temp.data;           head = head.next;           temp = temp.next;       }    }} 
0
vishalsavade1 week ago
//This code is contributed by Vishal Savade
void rearrangeEvenOdd(Node *head)
    {
       // Your Code here
       
       if(head == NULL || head->next == NULL || head->next->next == NULL) return;
       
       Node *ol = head;
       Node *el = head->next;
       Node *el_head = el;
       Node *t = head->next->next;
       
       int flag = 0;
       while(t!= NULL){
           if(flag == 0){
               ol->next = t;
               ol = ol->next;
               flag = 1;
           }
           else{
               el->next = t;
               el = el->next;
               flag = 0;
           }
           t = t->next;
       }
       ol->next = NULL;
       el->next = NULL;
       ol->next = el_head;
    }
+1
shubhamkhavare2 weeks ago
Simplest Java Code:
         Node temp = head;         Node res = new Node(0);         Node ans = res;         int count = 1;         while(temp != null)         {             if(count%2 != 0)             {                 res.next = new Node(temp.data);                 res = res.next;             }             count++;             temp = temp.next;         }         Node temp1 = head;         int count1 = 1;         while(temp1 != null)         {             if(count1%2 == 0)             {                 res.next = new Node(temp1.data);                 res = res.next;             }             count1++;             temp1 = temp1.next;         }         Node tempNode = ans.next;         while(tempNode != null)         {             head.data = tempNode.data;             head = head.next;             tempNode = tempNode.next;         }
0
cs200152 weeks ago


       Node *p=head;
       vector<int>v;
       while(p){
           v.push_back(p->data);
           p=p->next;
       }
       p=head;
       for(int i=0;i<v.size();i+=2){
           p->data=v[i];
           p=p->next;
       }
      for(int i=1;i<=v.size() && p!=NULL;i+=2){
          p->data=v[i];
          p=p->next;
      }
    }
Read Less
Reply Open Externally
0
best code  
+3
tanashah3 weeks ago
Guys it is,all odd position nodes are together and all even positions node are together.
Not a odd number and even number.
+2
badgujarsachin834 weeks ago
void rearrangeEvenOdd(Node *head)
    {
       // Your Code here
       Node* even=new Node(0);
       Node* odd=new Node(0);
       Node* e=even;
       Node* temp=head;
       Node* o=odd;
       int c=1;
       while(temp){
           if(c%2!=0){
               o->next=temp;
               o=o->next;
           }else{
               e->next=temp;
               e=e->next;
           }
           temp=temp->next;
           c++;
       }
       o->next=NULL;
       e->next=NULL;
       o->next=even->next;
       head=odd;
    }
0
hr061 month ago
class Solution
{
   public:
   void rearrangeEvenOdd(Node *head)
   {
      // Your Code here
      
      if(head == NULL || head->next == NULL)
      {
          return;
      }
      
      Node* odd = head;
      Node* oddH = odd;
      Node* even = head->next;
      Node* evenH = even;
      
      while(odd->next  && even->next)
      {
          odd->next = odd->next->next;
          even->next = even->next->next;
          
          odd = odd->next;
          even = even->next;
      }
      
      odd->next = evenH;
      head = oddH;
      
      
   }
};
0
rohanjaiswal200120182 months ago
void rearrangeEvenOdd(Node *head)   {      // Your Code here      Node* even=new Node(0);      Node* odd=new Node(0);      Node* temp=head, *e=even, *o=odd;      int c=1;      while(temp){          if(c%2!=0){ o->next=temp;o=o->next;                                      }          else{             e->next=temp;e=e->next;          }          c++;temp=temp->next;      }      o->next==NULL;e->next=NULL;      o->next=even->next;      head=odd;   }
void rearrangeEvenOdd(Node *head)   {      // Your Code here      Node* even=new Node(0);      Node* odd=new Node(0);      Node* temp=head, *e=even, *o=odd;      int c=1;      while(temp){          if(c%2!=0){ o->next=temp;o=o->next;                                      }          else{             e->next=temp;e=e->next;          }          c++;temp=temp->next;      }      o->next==NULL;e->next=NULL;      o->next=even->next;      head=odd;   }
0
lakshta192 months ago
   void rearrangeEvenOdd(Node *head)
    {
       Node *p=head;
       vector<int>v;
       while(p){
           v.push_back(p->data);
           p=p->next;
       }
       p=head;
       for(int i=0;i<v.size();i+=2){
           p->data=v[i];
           p=p->next;
       }
      for(int i=1;i<=v.size() && p!=NULL;i+=2){
          p->data=v[i];
          p=p->next;
      }
    }
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.