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 the 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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, char 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* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
}
void evenOdd( struct Node* head)
{
struct Node* temp = head;
deque< int > d;
while (temp != NULL) {
if (temp->data % 2 == 0)
d.push_front(temp->data);
else
d.push_back(temp->data);
temp = temp->next;
}
temp = head;
while (!d.empty()) {
temp->data = d.front();
d.pop_front();
temp = temp->next;
}
}
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;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void printList(Node head)
{
Node temp = head;
while (temp != null )
{
System.out.printf( "%d " ,
temp.data);
temp = temp.next;
}
}
static void evenOdd(Node head)
{
Node temp = head;
Deque<Integer> d =
new LinkedList<>();
while (temp != null )
{
if (temp.data % 2 == 0 )
d.addFirst(temp.data);
else
d.add(temp.data);
temp = temp.next;
}
temp = head;
while (!d.isEmpty())
{
temp.data = d.peek();
d.pollFirst();
temp = temp.next;
}
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 10 );
head = push(head, 9 );
head = push(head, 8 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
System.out.print( "Given linked list: " );
printList(head);
evenOdd(head);
System.out.print( "\nAfter rearrangement: " );
printList(head);
}
}
|
Python
import collections
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push( head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def printList( head):
temp = head
while (temp ! = None ):
print ( temp.data, end = " " )
temp = temp. next
def evenOdd( head):
temp = head
d = collections.deque([])
while (temp ! = None ) :
if (temp.data % 2 = = 0 ):
d.appendleft(temp.data)
else :
d.append(temp.data)
temp = temp. next
temp = head
while ( len (d) > 0 ) :
temp.data = d[ 0 ]
d.popleft()
temp = temp. next
head = None
head = push(head, 10 )
head = push(head, 9 )
head = push(head, 8 )
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 5 )
head = push(head, 4 )
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 1 )
print ( "Given linked list: " , end = "")
printList(head)
evenOdd(head)
print ( "\nAfter rearrangement: " , end = "")
printList(head)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void printList(Node head)
{
Node temp = head;
while (temp != null )
{
Console.Write( " " + temp.data);
temp = temp.next;
}
}
static void evenOdd(Node head)
{
Node temp = head;
List< int > d =
new List< int >();
while (temp != null )
{
if (temp.data % 2 == 0)
d.Insert(0, temp.data);
else
d.Add(temp.data);
temp = temp.next;
}
temp = head;
while (d.Count != 0)
{
temp.data = d[0];
d.RemoveAt(0);
temp = temp.next;
}
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 10);
head = push(head, 9);
head = push(head, 8);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
Console.Write( "Given linked list: " );
printList(head);
evenOdd(head);
Console.Write( "\nAfter rearrangement: " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function printList(head) {
var temp = head;
while (temp != null ) {
document.write( " " + temp.data);
temp = temp.next;
}
}
function evenOdd(head) {
var temp = head;
var d = [];
while (temp != null ) {
if (temp.data % 2 == 0) d.unshift(temp.data);
else d.push(temp.data);
temp = temp.next;
}
temp = head;
while (d.length != 0) {
temp.data = d[0];
d.shift();
temp = temp.next;
}
}
var head = null ;
head = push(head, 10);
head = push(head, 9);
head = push(head, 8);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
document.write( "Given linked list: " );
printList(head);
evenOdd(head);
document.write( "<br>After rearrangement: " );
printList(head);
</script>
|
OutputGiven linked list: 1 2 3 4 5 6 7 8 9 10
After rearrangement: 10 8 6 4 2 1 3 5 7 9
Complexity Analysis:
- Time complexity: O(N)
- Auxiliary Space: O(N), where N is the total number of nodes in the linked list.