JavaScript Program to Print Odd Numbers in a Linked List
Last Updated :
29 Feb, 2024
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, 19
This recursive method checks each node in the linked list to determine if the current node's data represents an odd number, and if so, it is printed.
Example: This example shows the use of the above-explained approach.
JavaScript
// JavaScript program to print odd numbers
// in a linked list using recursive approach
// Node class represents each element in
// the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// LinkedList class manages the
// linked list operations
class LinkedList {
constructor() {
this.head = null;
}
// Method to add a new node at the
//beginning of the linked list
addNode(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
// Recursive method to print
// odd numbers in the linked list
oddNumbers(current) {
// Base case: if current node is null, return
if (current === null) {
return;
}
// Check if the data of the current node
// is odd, and print if true
if (current.data % 2 !== 0) {
console.log(current.data);
}
// Recursive call for the next node in the linked list
this.oddNumbers(current.next);
}
}
// Example
let linkedList = new LinkedList();
linkedList.addNode(1);
linkedList.addNode(4);
linkedList.addNode(7);
linkedList.addNode(9);
linkedList.addNode(2);
// Displaying odd numbers in the linked list
// using the recursive approach
console.log(
"Odd numbers in the linked list using recursive approach:");
linkedList.oddNumbers(linkedList.head);
OutputOdd numbers in the linked list using recursive approach:
9
7
1
Time Complexity: O(n)
Space Complexity: O(n)
To print odd numbers in a linked list with the while loop method, we go through each node step by step until we reach the end of the list. At each node, we check whether the node's data is odd, and if so, we display it.
Example: This example shows the use of the above-explained approach.
JavaScript
// JavaScript program to print
// odd numbers in a linked list using while loop
// Node class represents each element in the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// LinkedList class manages the linked list operations
class LinkedList {
constructor() {
this.head = null;
}
// Method to add a new node at the
// beginning of the linked list
addNode(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
// Method to print odd numbers in the
// linked list using while loop
oddNumbers() {
let current = this.head;
// While loop to iterate through the linked list
while (current !== null) {
// Check if the data of the current
// node is odd, and print if true
if (current.data % 2 !== 0) {
console.log(current.data);
}
current = current.next;
}
}
}
// Example
let linkedList = new LinkedList();
linkedList.addNode(2);
linkedList.addNode(11);
linkedList.addNode(4);
linkedList.addNode(8);
linkedList.addNode(9);
// Displaying odd numbers in the linked list
// using the while loop approach
console.log("Odd numbers in the linked list:");
linkedList.oddNumbers();
OutputOdd numbers in the linked list:
9
11
Time Complexity: O(n)
Space Complexity: O(1)
This method uses the same approach as above, but with the use of a 'for loop' instead of a 'while loop'.
Example: This example shows the use of the above-explained approach.
JavaScript
// JavaScript program to print odd numbers
// in a linked list using for loop
// Node class represents each element in the linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// LinkedList class manages the linked list operations
class LinkedList {
constructor() {
this.head = null;
}
// Method to add a new node at the beginning
// of the linked list
addNode(data) {
let newNode = new Node(data);
newNode.next = this.head;
this.head = newNode;
}
// Method to print odd numbers in the
// linked list using for loop
printOddNumbers() {
// For loop to iterate through the linked list
for (let current = this.head; current !== null; current = current.next) {
// Check if the data of the current node is odd, and print if true
if (current.data % 2 !== 0) {
console.log(current.data);
}
}
}
}
// Example
let linkedList = new LinkedList();
linkedList.addNode(1);
linkedList.addNode(9);
linkedList.addNode(3);
linkedList.addNode(4);
linkedList.addNode(7);
// Displaying odd numbers in the linked list
// using the for loop approach
console.log("Odd numbers in the linked list:");
linkedList.printOddNumbers();
OutputOdd numbers in the linked list:
7
3
9
1
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
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 Negative Numbers in a Linked List 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 encountere
2 min read
JavaScript Program to Print all Even Numbers in a Sorted Order of a Linked List 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
3 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 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
How to Sort a LinkedList in Java? A Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations.Sorting the nodes of a Singly Linked list in ascending order:Original ListSorted ListWe can sort the LinkedList by many sorting techniques:Selection SortInsertion sortQuick sortMerge sort Me
13 min read
C# Program to Generate Odd Numbers in Parallel using LINQ LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matt
2 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 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