JavaScript Program to Print Negative Numbers in a Linked List Last Updated : 26 Feb, 2024 Comments Improve Suggest changes Like Article Like Report This JavaScript program aims to print negative numbers present in 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 the negative numbers encountered. Table of Content Iterative ApproachRecursive ApproachIterative ApproachThe elements of the linked list are traversed using a loop and each one of them is checked for the negative value. It prints the negative values on the console. Example: The below example prints negative numbers in a 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 printNegativeNumbersIterative(head) { let current = head; while (current !== null) { if (current.value < 0) { console.log(current.value); } current = current.next; } } const linkedList = createLinkedList([-1, 2, -3, 4, -5]); printNegativeNumbersIterative(linkedList.head); Output-1 -3 -5 Recursive ApproachImplement a recursive function to traverse the linked list, checking each element recursively and printing the negative numbers encountered. Example: The below example is to print negative numbers in a 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 printNegativeNumbersRecursive(node) { if (node === null) { return; } if (node.value < 0) { console.log(node.value); } printNegativeNumbersRecursive(node.next); } const linkedList = createLinkedList([-1, 2, -3, 4, -5]); printNegativeNumbersRecursive(linkedList.head); Output-1 -3 -5 Comment More infoAdvertise with us Next Article JavaScript Program to Print Negative Numbers in a Linked List A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 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 Positive Numbers in a Linked List This JavaScript program aims to print positive numbers present in 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 the positive numbers encountere 2 min read 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 Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no 5 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 C# Program to Find the Negative Double Numbers From the List of Objects Using LINQ Given a list of objects, we need to find the negative double from the list of objects, this task can be done using the OfType() method along with the Where() method. OfType() method is used to filter the elements of an IEnumerable based on a specified type. Or in other words, this method is used to 2 min read Javascript Program for Clockwise rotation of Linked List Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, K = 2 Output: 4 -> 5 -> 1 -> 2 -> 3 -> NULLInput: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL 4 min read Javascript Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit 2 min read Reverse a LinkedList in Java Assuming you have gone through LinkedList in java and know about linked list. This post contains different examples for reversing a linked list which are given below: 1. By writing our own function(Using additional space): reverseLinkedList() method contains logic for reversing string objects in a l 7 min read Like