Segregate even and odd nodes in a Linked List using Deque

Given a Linked List of integers. The task is to write a program to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. It is not needed to keep the order of even and odd nodes same as that of the original list, the task is just to rearrange the nodes such that all even valued nodes appear before the odd valued nodes.

See Also: Segregate even and odd nodes in a Linked List

Examples:



Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL
Output: 10 -> 8 -> 6 -> 4 -> 2 -> 1 -> 3 -> 5 -> 7 -> 9 -> NULL

Input: 4 -> 3 -> 2 -> 1 -> NULL
Output: 2 -> 4 -> 3 -> 1 -> NULL

The idea is to iteratively push all the elements of the linked list to deque as per the below conditions:

  • Start traversing the linked list and if an element is even then push it to the front of the Deque and,
  • If the element is odd then push it to the back of the Deque.

Finally, replace all elements of the linked list with the elements of Deque starting from the first element.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to segregate even and
// odd noeds in a linked list using deque
#include <bits/stdc++.h>
using namespace std;
  
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
  
/*UTILITY FUNCTIONS*/
/* Push a node to linked list. Note that this function
changes the head */
void push(struct Node** head_ref, char new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  
    /* put in the data */
    new_node->data = new_data;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* move the head to pochar to the new node */
    (*head_ref) = new_node;
}
  
// printing the linked list
void printList(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
                printf("%d ", temp->data);
        temp = temp->next;
    }
}
  
// Function to rearrange even and odd
// elements in a linked list using deque
void evenOdd(struct Node* head)
{
    struct Node* temp = head;
      
    // Declaring a Deque
    deque<int> d; 
  
    // Push all the elements of
    // linked list in to deque
    while (temp != NULL) {
  
        // if element is even push it
        // to front of the deque
        if (temp->data % 2 == 0) 
            d.push_front(temp->data);
  
        else // else push at the back of the deque
            d.push_back(temp->data);
        temp = temp->next; // increase temp
    }
      
    temp = head;
      
    // Replace all elements of the linked list
    // with the elements of Deque starting from 
    // the first element
    while (!d.empty()) {
        temp->data = d.front();
        d.pop_front();
        temp = temp->next; 
    }
}
  
// Driver code
int main()
{
    struct Node* head = NULL;
    push(&head, 10);
    push(&head, 9);
    push(&head, 8);
    push(&head, 7);
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
      
    cout << "Given linked list: ";
        printList(head);
          
    evenOdd(head);
      
    cout << "\nAfter rearrangement: ";
        printList(head);
          
    return 0;
}

chevron_right


Output:

Given linked list: 1 2 3 4 5 6 7 8 9 10 
After rearrangement: 10 8 6 4 2 1 3 5 7 9

Time complexity: O(N)
Auxiliary Space: O(N), where N is the total number of nodes in the linked list.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.