📌  相关文章
📜  通过用最近的 K 倍数替换每个节点来修改链表

📅  最后修改于: 2021-09-06 11:25:11             🧑  作者: Mango

给定一个由N 个节点和一个整数K组成的单链表L ,任务是将给定链表的每个节点的值修改为其最近的K倍数,不超过该节点的值。

例子:

方法:给定的问题可以通过遍历给定的链表来解决,对于遍历的每个节点,找到节点值除以K 的下限值,比如说val ,并将节点值更新为val*K

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Structure of node
struct node {
    int data;
    node* next;
};
 
// Function to replace the node N
// by the nearest multiple of K
node* EvalNearestMult(node* N, int K)
{
    node* temp = N;
    int t;
 
    // Traverse the Linked List
    while (N != NULL) {
 
        // If data is less than K
        if (N->data < K)
            N->data = 0;
 
        else {
 
            // If the data of current
            // node is same as K
            if (N->data == K)
                N->data = K;
 
            // Otherwise change the value
            else {
                N->data = (N->data / K) * K;
            }
        }
 
        // Move to the next node
        N = N->next;
    }
 
    // Return the updated LL
    return temp;
}
 
// Function to print the nodes of
// the Linked List
void printList(node* N)
{
    // Traverse the LL
    while (N != NULL) {
 
        // Print the node's data
        cout << N->data << "  ";
        N = N->next;
    }
}
 
// Driver Code
int main()
{
    // Given Linked List
    node* head = NULL;
    node* second = NULL;
    node* third = NULL;
 
    head = new node();
    second = new node();
    third = new node();
 
    head->data = 3;
    head->next = second;
    second->data = 4;
    second->next = third;
    third->data = 8;
    third->next = NULL;
 
    node* t = EvalNearestMult(head, 3);
 
    printList(t);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Structure of node
    static class node {
        int data;
        node next;
 
        node(int d)
        {
            data = d;
        }
    }
 
    // Function to replace the node N
    // by the nearest multiple of K
    static node EvalNearestMult(node N, int K)
    {
        node temp = N;
        int t;
 
        // Traverse the Linked List
        while (N != null) {
 
            // If data is less than K
            if (N.data < K)
                N.data = 0;
 
            else {
 
                // If the data of current
                // node is same as K
                if (N.data == K)
                    N.data = K;
 
                // Otherwise change the value
                else {
                    N.data = (N.data / K) * K;
                }
            }
 
            // Move to the next node
            N = N.next;
        }
 
        // Return the updated LL
        return temp;
    }
 
    // Function to print the nodes of
    // the Linked List
    static void printList(node N)
    {
        // Traverse the LL
        while (N != null) {
 
            // Print the node's data
            System.out.print(N.data + "  ");
            N = N.next;
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given Linked List
        node head = new node(3);
        head.next = new node(5);
          head.next.next = new node(8);
 
        node t = EvalNearestMult(head, 3);
 
        printList(t);
    }
}
 
// This code is contributed by Dharanendra L V


输出:
3  3  6

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live