Javascript Program For Segregating Even And Odd Nodes In A Linked List
Last Updated :
09 Sep, 2024
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:
- 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.
JavaScript
// Javascript program to segregate even and
// odd nodes in a Linked List
// Head of list
let head;
// Linked list Node
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
function segregateEvenOdd() {
let end = head;
let prev = null;
let curr = head;
// Get pointer to last Node
while (end.next != null)
end = end.next;
let 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 */
let 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() {
let temp = head;
while (temp != null) {
console.log(temp.data);
temp = temp.next;
}
console.log();
}
// Driver code
push(11);
push(10);
push(8);
push(6);
push(4);
push(2);
push(0);
console.log("Original Linked List ");
printList();
segregateEvenOdd();
console.log("Modified Linked List ");
printList();
// This code is contributed by umadevi9616
OutputOriginal Linked List
0
2
4
6
8
10
11
Modified Linked List
0
2
4
6
8
10
11
Complexity Analysis:
- 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
// JavaScript program to segregate
// even and odd nodes in a Linked List
// Head of list
let head;
// Linked list Node
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
function segregateEvenOdd() {
let evenStart = null;
let evenEnd = null;
let oddStart = null;
let oddEnd = null;
let currentNode = head;
while (currentNode != null) {
let 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*/
let 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() {
let temp = head;
while (temp != null) {
console.log(temp.data + " ");
temp = temp.next;
}
}
// Driver code
push(11);
push(10);
push(9);
push(6);
push(4);
push(1);
push(0);
console.log("Original Linked List");
printList();
segregateEvenOdd();
console.log("Modified Linked List");
printList();
// This code is contributed by todaysgaurav
OutputOriginal Linked List
0
1
4
6
9
10
11
Modified Linked List
0
4
6
10
1
9
11
Complexity Analysis:
- 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!
Similar Reads
JavaScript Linked List Programs JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesnât store the elements contiguously like an array. S. NoArticles1JavaScript Program
5 min read
Implementation of LinkedList in Javascript In this article, we will be implementing the LinkedList data structure in Javascript.A linked list is a linear data structure where elements are stored in nodes, each containing a value and a reference (or pointer) to the next node. It allows for efficient insertion and deletion operations.Each node
5 min read
Javascript Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise.bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functio
3 min read
Javascript Program For Inserting A Node In A Linked List We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. JavaScript// Linked List Class // Head of list let head; // Node
7 min read
Javascript Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node.Examples: Input : list: 1->2->4->5 x = 3Output : 1->2->3-
4 min read
Javascript Program For Writing A Function To Delete A Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. This article focuses on writing a function to delete a linked list.Implementation: JavaScript// Javascript program to delete // a li
1 min read
Javascript Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position.Example: Input: position = 1, Linked List = 8->2->3->1->7Output: Linked List = 8->3->1->7Input: position = 0, Linked List = 8->2->3->1->7Output: Linked List = 2->3->1-
3 min read
Javascript Program For Finding Length Of A Linked List Write a function to count the number of nodes in a given singly linked list.For example, the function should return 5 for linked list 1->3->1->2->1.Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head.3) Do following while current is not NULL a) curre
3 min read
Javascript Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal
5 min read
Javascript Program For Making Middle Node Head In A Linked List Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples:Input: 1 2 3 4 5 Output: 3 1 2 4 5Input: 1 2 3 4 5 6Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves on
3 min read