Open In App

Javascript Program For Segregating Even And Odd Nodes In A Linked List

Improve
Improve
Like Article
Like
Save
Share
Report

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 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 1: 
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:  

  1. Get pointer to the last node.
  2. 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.

Javascript




<script>
// Javascript program to segregate even and
// odd nodes in a Linked List
 
// Head of list
var head;
 
// Linked list Node
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
 
function segregateEvenOdd()
{
    var end = head;
    var prev = null;
    var curr = head;
 
    // Get pointer to last Node
    while (end.next != null)
        end = end.next;
 
    var new_end = end;
 
    // Consider all odd nodes before
    // getting first even node
    while (curr.data % 2 != 0 &&
           curr != end)
    {
        new_end.next = curr;
        curr = curr.next;
        new_end.next.next = null;
        new_end = new_end.next;
    }
 
    // Do following steps only if
    // there is an even node
    if (curr.data % 2 == 0)
    {
        head = curr;
 
        // Now curr points to first
        // even node
        while (curr != end)
        {
            if (curr.data % 2 == 0)
            {
                prev = curr;
                curr = curr.next;
            }
            else
            {
                // Break the link between prev
                // and curr
                prev.next = curr.next;
 
                // Make next of curr as null
                curr.next = null;
 
                // Move curr to end
                new_end.next = curr;
 
                // Make curr as new end of list
                new_end = curr;
 
                // Update curr pointer
                curr = prev.next;
            }
        }
    }
 
    /* We have to set prev before executing
       rest of this code */
    else
        prev = curr;
 
    if (new_end != end &&
        end.data % 2 != 0)
    {
        prev.next = end.next;
        end.next = null;
        new_end.next = end;
    }
}
 
/* Given a reference (pointer to pointer) to
   the head of a list and an int, push
   a new node on the front of the list */
function push(new_data)
{
    /* 1 & 2: Allocate the Node &
              Put in the data */
    var new_node = new Node(new_data);
 
    /* 3. Make next of new Node as
          head */
    new_node.next = head;
 
    /* 4. Move the head to point
          to new Node */
    head = new_node;
}
 
// Utility function to print
// a linked list
function printList()
{
    var temp = head;
    while (temp != null)
    {
        document.write(temp.data + " ");
        temp = temp.next;
    }
    document.write();
}
 
//  Driver code
push(11);
push(10);
push(8);
push(6);
push(4);
push(2);
push(0);
document.write(
         "Original Linked List ");
 
printList();
document.write("<br>");
 
segregateEvenOdd();
document.write(
         "Modified Linked List ");
// This code is contributed by umadevi9616
</script>


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)

Method 2: 
The idea is to split the linked list into two: one containing all even nodes and other containing all odd nodes. And finally, attach the odd node linked list after the even node linked list. 
To split the Linked List, traverse the original Linked List and move all odd nodes to a separate Linked List of all odd nodes. At the end of the loop, the original list will have all the even nodes and the odd node list will have all the odd nodes. To keep the ordering of all nodes the same, we must insert all the odd nodes at the end of the odd node list. And to do that in constant time, we must keep track of the last pointer in the odd node list.

Javascript




<script>
// JavaScript program to segregate
// even and odd nodes in a Linked List
     
// Head of list
var head; 
  
// Linked list Node
class Node
{
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}
     
function segregateEvenOdd()
{       
    var evenStart = null;
    var evenEnd = null;
    var oddStart = null;
    var oddEnd = null;
    var currentNode = head;
         
    while(currentNode != null)
    {
        var element = currentNode.data;
             
        if(element % 2 == 0)
        {               
            if(evenStart == null)
            {
                evenStart = currentNode;
                evenEnd = evenStart;
             }
             else
             {
                 evenEnd.next = currentNode;
                 evenEnd = evenEnd.next;
              }
                 
        }
        else
        {               
            if(oddStart == null)
            {
                oddStart = currentNode;
                oddEnd = oddStart;
            }
            else
            {
                oddEnd.next = currentNode;
                oddEnd = oddEnd.next;
            }
        }
         
        // Move head pointer one step in
        // forward direction
        currentNode = currentNode.next;   
    }       
         
    if(oddStart == null ||
       evenStart == null)
    {
        return;
    }
         
    evenEnd.next = oddStart;
    oddEnd.next = null;
    head=evenStart;
}
     
/* Given a reference (pointer to pointer)
   to the head of a list and an int, push
   a new node on the front of the list. */
function push(new_data)
{
    /* 1 & 2: Allocate the Node &
              Put in the data*/
    var new_node = new Node(new_data);
  
    // 3. Make next of new Node as head
    new_node.next = head;
  
    // 4. Move the head to point to new Node
    head = new_node;
}
  
// Utility function to print a linked list
function printList()
{
    var temp = head;
    while(temp != null)
    {
        document.write(temp.data+" ");
        temp = temp.next;
    }
    document.write("<br/>");
}
     
// Driver code   
push(11);
push(10);
push(9);
push(6);
push(4);
push(1);
push(0);
document.write(
         "Original Linked List<br/>");
printList();
  
segregateEvenOdd();
  
document.write(
         "Modified Linked List<br/>");
printList();
// This code is contributed by todaysgaurav
</script>


Output:

Original Linked List
0 1 4 6 9 10 11 
Modified Linked List
0 4 6 10 1 9 11 

Time complexity: O(n)
Auxiliary space: O(1) because using constant space

Please refer complete article on Segregate even and odd nodes in a Linked List for more details!



Last Updated : 07 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads