Open In App

JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This JavaScript program is designed to identify all even numbers in a sorted order of a linked list. A linked list is a linear data structure where elements, known as nodes, are connected via pointers.

The program iterates through the linked list to determine the all-even numbers in the sorted order it contains.

There are several ways to print all even numbers in a sorted order of a linked list in JavaScript which are as follows:

Using Iterative Approach

The elements of the linked list are traversed using a loop and within this loop, each element of the list is checked to determine if it’s even using the condition if num % 2 == 0. If the current number is even, it is printed using the console.log() function. This loop ensures that all even values present in the list are printed on the console.

Example: The below example prints even numbers in a sorted linked list in JavaScript.

Javascript




class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}
 
function createLinkedList(arr) {
    if (arr.length === 0) {
        return null;
    }
 
    let head = new Node(arr[0]);
    let current = head;
 
    for (let i = 1; i < arr.length; i++) {
        current.next = new Node(arr[i]);
        current = current.next;
    }
 
    return { head };
}
 
function printEvenNumbersIterative(head) {
    let current = head;
    while (current !== null) {
        if (current.value % 2 == 0) {
            console.log(current.value);
        }
        current = current.next;
    }
}
const linkedList = createLinkedList(
    [
        1, 2, 3, 4, 5
    ]);
printEvenNumbersIterative(linkedList.head);


Output

2
4

Using recursive approach

Implement a recursive function to traverse the linked list, checking each element recursively and if the current number is even, it is printed using the console.log() function. Then again recursive call which ensures that all even values present in the list are printed on the console using recursion.

Example: The below example is to print even numbers in a sorted linked list in JavaScript.

Javascript




class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}
 
function createLinkedList(arr) {
    if (arr.length === 0) {
        return null;
    }
 
    let head = new Node(arr[0]);
    let current = head;
 
    for (let i = 1; i < arr.length; i++) {
        current.next = new Node(arr[i]);
        current = current.next;
    }
 
    return { head };
}
 
function printEvenNumbersRecursive(node) {
    if (node === null) {
        return;
    }
    if (node.value % 2 === 0) {
        console.log(node.value);
    }
    printEvenNumbersRecursive(node.next);
}
const linkedList = createLinkedList([1, 2, 3, 4, 5]);
printEvenNumbersRecursive(linkedList.head);


Output

2
4


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads