C Program For Segregating Even And Odd Nodes In A Linked List
Last Updated :
28 Dec, 2022
Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers the same.
Examples:
Input: 17->15->8->12->10->5->4->1->7->6->NULL
Output: 8->12->10->4->6->17->15->5->1->7->NULL
Input: 8->12->10->5->4->1->6->NULL
Output: 8->12->10->4->6->5->1->NULL
// If all numbers are even then do not change the list
Input: 8->12->10->NULL
Output: 8->12->10->NULL
// If all numbers are odd then do not change the list
Input: 1->3->5->7->NULL
Output: 1->3->5->7->NULL
Method:
The idea is to get pointer to the last node of list. And then traverse the list starting from the head node and move the odd-valued nodes from their current position to end of the list.
Thanks to blunderboy for suggesting this method. Algorithm:
- Get pointer to the last node.
- Move all the odd nodes to the end.
- Consider all odd nodes before the first even node and move them to end.
- Change the head pointer to point to the first even node.
- Consider all odd nodes after the first even node and move them to the end.
Below is the implementation of the above approach:
C
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void segregateEvenOdd( struct Node **head_ref)
{
struct Node *end = *head_ref;
struct Node *prev = NULL;
struct Node *curr = *head_ref;
while (end->next != NULL)
end = end->next;
struct Node *new_end = end;
while (curr->data % 2 != 0 &&
curr != end)
{
new_end->next = curr;
curr = curr->next;
new_end->next->next = NULL;
new_end = new_end->next;
}
if (curr->data % 2 == 0)
{
*head_ref = curr;
while (curr != end)
{
if ( (curr->data) % 2 == 0 )
{
prev = curr;
curr = curr->next;
}
else
{
prev->next = curr->next;
curr->next = NULL;
new_end->next = curr;
new_end = curr;
curr = prev->next;
}
}
}
else prev = curr;
if (new_end != end &&
(end->data) % 2 != 0)
{
prev->next = end->next;
end->next = NULL;
new_end->next = end;
}
return ;
}
void push( struct Node** head_ref,
int new_data)
{
struct Node* new_node =
( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList( struct Node *node)
{
while (node!=NULL)
{
printf ( "%d " , node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 11);
push(&head, 10);
push(&head, 8);
push(&head, 6);
push(&head, 4);
push(&head, 2);
push(&head, 0);
printf ( "Original Linked list " );
printList(head);
segregateEvenOdd(&head);
printf ( "Modified Linked list " );
printList(head);
return 0;
}
|
Output
Original Linked list 0 2 4 6 8 10 11 Modified Linked list 0 2 4 6 8 10 11
Time complexity: O(n)
Auxiliary space: O(1) as it is using constant space
Please refer complete article on Segregate even and odd nodes in a Linked List for more details!
Share your thoughts in the comments
Please Login to comment...