![]() |
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 LoopTo print even numbers in a linked list using the while loop method, we go through each element one by one until we reach the end. At each step, we check if the number is even or not, and if it is even, we print it. Example: The below code implements the while loop to print even numbers in a linked list. Javascript
Output
Even numbers in the linked list: 2 8 10 Time Complexity: O(n) Space Complexity: O(1) Using RecursionThis method recursively checks each node in the linked list to determine if current node’s data stores an even number, if it is even then it simply print it. Example: The below code implements recursion to check for the even numers in the linked list. Javascript
Output
Even numbers in the linked list are: 2 4 Time Complexity: O(n) Space Complexity: O(n) |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |