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
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
class LinkedList {
constructor() {
this .head = null ;
}
addNode(data) {
let newNode = new Node(data);
newNode.next = this .head;
this .head = newNode;
}
oddNumbers(current) {
if (current === null ) {
return ;
}
if (current.data % 2 !== 0) {
console.log(current.data);
}
this .oddNumbers(current.next);
}
}
let linkedList = new LinkedList();
linkedList.addNode(1);
linkedList.addNode(4);
linkedList.addNode(7);
linkedList.addNode(9);
linkedList.addNode(2);
console.log(
"Odd numbers in the linked list using recursive approach:" );
linkedList.oddNumbers(linkedList.head);
|
Output
Odd 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
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
class LinkedList {
constructor() {
this .head = null ;
}
addNode(data) {
let newNode = new Node(data);
newNode.next = this .head;
this .head = newNode;
}
oddNumbers() {
let current = this .head;
while (current !== null ) {
if (current.data % 2 !== 0) {
console.log(current.data);
}
current = current.next;
}
}
}
let linkedList = new LinkedList();
linkedList.addNode(2);
linkedList.addNode(11);
linkedList.addNode(4);
linkedList.addNode(8);
linkedList.addNode(9);
console.log( "Odd numbers in the linked list:" );
linkedList.oddNumbers();
|
Output
Odd 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
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
class LinkedList {
constructor() {
this .head = null ;
}
addNode(data) {
let newNode = new Node(data);
newNode.next = this .head;
this .head = newNode;
}
printOddNumbers() {
for (let current = this .head; current !== null ; current = current.next) {
if (current.data % 2 !== 0) {
console.log(current.data);
}
}
}
}
let linkedList = new LinkedList();
linkedList.addNode(1);
linkedList.addNode(9);
linkedList.addNode(3);
linkedList.addNode(4);
linkedList.addNode(7);
console.log( "Odd numbers in the linked list:" );
linkedList.printOddNumbers();
|
Output
Odd numbers in the linked list:
7
3
9
1
Time Complexity: O(n)
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...