![]() |
Given a linked list, the task is to replace every node with its closest prime number and return the modified list. Examples:
Approach: To solve the problem follow the below idea:
Steps of this approach:
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output
Original List : 1 -> 2 -> 8 -> 7 -> 14 -> NULL Modified List : 2 -> 2 -> 7 -> 7 -> 13 -> NULL Time Complexity: O(n * sqrt(n)) where n is the length of the linked list. This is because we are checking for primes in the closestPrime() function. |
Reffered: https://www.geeksforgeeks.org
DSA |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |