JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List Last Updated : 04 Mar, 2024 Comments Improve Suggest changes Like Article Like 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: Table of Content Using Iterative ApproachUsing recursive approachUsing Iterative ApproachThe 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); Output2 4 Using recursive approachImplement 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); Output2 4 Comment More infoAdvertise with us Next Article JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to Print all Odd Elements in a Sorted Order of a Linked List This JavaScript program aims to print all odd elements in a sorted order of a linked list. A linked list is a data structure consisting of a sequence of elements, where each element points to the next element in the sequence. The program traverses the linked list and prints all odd elements in a sor 2 min read JavaScript Program to Print Even Numbers in a Linked List A linked list is a data structure that stores the values at different memory locations concerning the next memory block of stored value. You can get all the even numbers stored in a linked list using the below methods. Table of Content Using While LoopUsing RecursionUsing While LoopTo print even num 2 min read JavaScript Program to Print Odd Numbers in a Linked List This article will show you different ways to find and print all the odd numbers in a linked list using JavaScript. Example: Input: 2->1->9->3 Output: 1, 9, 3 Input: 5->5->1->0->19->8 Output: 5, 5, 1, 19Table of Content Using Recursive ApproachUsing While LoopUsing For LoopUsi 4 min read Javascript Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where 7 min read Javascript Program To Check Whether The Length Of Given Linked List Is Even Or Odd Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd. Examples:Input : 1->2->3->4->NULLOutput : EvenInput : 1->2->3->4->5->NULLOutput : OddMethod 1: Count the codes linearly Traverse the entire Linked List an 3 min read Javascript Program For Sorting A Linked List Of 0s, 1s And 2s Given a linked list of 0s, 1s and 2s, sort it.Examples:Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULLOutput: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULLInput: 1 -> 1 -> 2 -> 1 -> 0 -> NULL Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULLSource 3 min read Javascript Program For Rearranging A Linked List Such That All Even And Odd Positioned Nodes Are Together Rearrange a linked list in such a way that all odd position nodes are together and all even positions node are together, Examples: Input: 1->2->3->4Output: 1->3->2->4Input: 10->22->30->43->56->70Output: 10->30->56->22->43->70The important thing in this 3 min read Java Program to Create a Singly Linked List and Count the Number of Nodes Linked List is a linear data structure. Linked list elements are not stored at a contiguous location, the elements are linked using pointers. Singly Linked list is the collection of nodes, where each node has two parts one is the data and other is the linked part. Example: Input : AddNodes = {2, 3, 3 min read Java Program to Return the Elements at Odd Positions in a List Given a List, the task is to return the elements at Odd positions in a list. Let's consider the following list.Clearly, we can see that elements 20, 40, 60 are at Odd Positions as the index of the list is zero-based. Now we should return these elements.Approach 1:Initialize a temporary value with ze 3 min read Javascript Program For Sorting Linked List Which Is Already Sorted On Absolute Values Given a linked list that is sorted based on absolute values. Sort the list based on actual values.Examples: Input: 1 -> -10 Output: -10 -> 1Input: 1 -> -2 -> -3 -> 4 -> -5 Output: -5 -> -3 -> -2 -> 1 -> 4 Input: -5 -> -10 Output: -10 -> -5Input: 5 -> 10 Output: 3 min read Like