原文:https://www.geeksforgeeks.org/product-of-all-nodes-in-a-doubly-linked-list-divisible-by-a-given-number-k/

给定一个包含n个节点的双链表,并给定数字k。任务是找到所有可被k整除的节点的乘积。

示例

input : list = 15 <=> 16 <=> 10 <=> 9 <=> 6 <=> 7 <=> 17
        k = 3
output : product = 810
input : list = 5 <=> 3 <=> 6 <=> 8 <=> 4 <=> 1 <=> 2 <=> 9
        k = 2
output : product = 384

这个想法是遍历双链表并一一检查节点。 如果某个节点的值可被k整除,则将该节点的值乘以到目前为止的乘积,并在未到达列表末尾的情况下继续此过程。

下面是上述方法的实现:

c

// c   program to find product of nodes in a 
// doubly linked list divisible by k 
#include  
using namespace std; 
// doubly linked list node 
struct node { 
    int data; 
    node *prev, *next; 
}; 
// function to insert a node at the beginning 
// of the doubly linked list 
void push(node** head_ref, int new_data) 
{ 
    // allocate node 
    node* new_node = (node*)malloc(sizeof(struct node)); 
    // put in the data 
    new_node->data = new_data; 
    // since we are adding at the beginning, 
    // prev is always null 
    new_node->prev = null; 
    // link the old list off the new node 
    new_node->next = (*head_ref); 
    // change prev of head node to new node 
    if ((*head_ref) != null) 
        (*head_ref)->prev = new_node; 
    // move the head to point to the new node 
    (*head_ref) = new_node; 
} 
// function to find the product of all the nodes from 
// the doubly linked list that is divisible by k 
int productofnode(node** head_ref, int k) 
{ 
    node* ptr = *head_ref; 
    node* next; 
    int product = 1; 
    // travese list till last node 
    while (ptr != null) { 
        next = ptr->next; 
        // check is node value divided by k 
        // if true then add in sum 
        if (ptr->data % k == 0) 
            product *= ptr->data; 
        ptr = next; 
    } 
    // return product of nodes which 
    // are divisible by k 
    return product; 
} 
// driver code 
int main() 
{ 
    // start with the empty list 
    node* head = null; 
    // create the doubly linked list 
    // 15 16 10 9 6 7 17 
    push(&head, 17); 
    push(&head, 7); 
    push(&head, 6); 
    push(&head, 9); 
    push(&head, 10); 
    push(&head, 16); 
    push(&head, 15); 
    int k = 3; 
    int prod = productofnode(&head, k); 
    cout << "product = " << prod; 
    return 0; 
} 

java

// java program to find product of nodes in a  
// doubly linked list divisible by k  
class gfg 
{ 
// doubly linked list node  
static class node 
{  
    int data;  
    node prev, next;  
};  
// function to insert a node at the beginning  
// of the doubly linked list  
static node push(node head_ref, int new_data)  
{  
    // allocate node  
    node new_node = new node();  
    // put in the data  
    new_node.data = new_data;  
    // since we are adding at the beginning,  
    // prev is always null  
    new_node.prev = null;  
    // link the old list off the new node  
    new_node.next = (head_ref);  
    // change prev of head node to new node  
    if ((head_ref) != null)  
        (head_ref).prev = new_node;  
    // move the head to point to the new node  
    (head_ref) = new_node;  
    return head_ref; 
}  
// function to find product of all the nodes from  
// the doubly linked list that are divisible by k  
static int productofnode(node head_ref, int k)  
{  
    node ptr = head_ref;  
    node next;  
    int product = 1;  
    // travese list till last node  
    while (ptr != null) 
    {  
        next = ptr.next;  
        // check is node value divided by k  
        // if true then add in sum  
        if (ptr.data % k == 0)  
            product *= ptr.data;  
        ptr = next;  
    }  
    // return product of nodes which  
    // are divisible by k  
    return product;  
}  
// driver code  
public static void main(string args[]) 
{  
    // start with the empty list  
    node head = null;  
    // create the doubly linked list  
    // 15 16 10 9 6 7 17  
    head = push(head, 17);  
    head = push(head, 7);  
    head = push(head, 6);  
    head = push(head, 9);  
    head = push(head, 10);  
    head = push(head, 16);  
    head = push(head, 15);  
    int k = 3;  
    int prod = productofnode(head, k);  
    system.out.println( "product = "   prod);  
} 
}  
// this code is contributed by arnab kundu 

python3

# python3 program to find product of nodes in a 
# doubly linked list divisible by k 
# node of the doubly linked list  
class node:  
    def __init__(self, data):  
        self.data = data  
        self.prev = none
        self.next = none
# function to insert a node at the beginning 
# of the doubly linked list 
def push(head_ref, new_data): 
    # allocate node 
    new_node = node(0) 
    # put in the data 
    new_node.data = new_data 
    # since we are multiplying at the beginning, 
    # prev is always none 
    new_node.prev = none
    # link the old list off the new node 
    new_node.next = (head_ref) 
    # change prev of head node to new node 
    if ((head_ref) != none): 
        (head_ref).prev = new_node 
    # move the head to point to the new node 
    (head_ref) = new_node 
    return head_ref 
# function to product all the nodes 
# from the doubly linked 
# list that are divided by k 
def productofnode(head_ref, k): 
    ptr = head_ref 
    next = none
    # variable product=1  
    product = 1
    # traves list till last node 
    while (ptr != none) : 
        next = ptr.next
        # check is node value divided by k 
        # if true then multiply in product 
        if (ptr.data % k == 0): 
            product *= ptr.data 
        ptr = next
    # return product of nodes which is divided by k 
    return product 
# driver code 
if __name__ == "__main__":  
    # start with the empty list 
    head = none
    # create the doubly linked list 
    # 15 <. 16 <. 10 <. 9 <. 6 <. 7 <. 17 
    head = push(head, 17) 
    head = push(head, 7) 
    head = push(head, 6) 
    head = push(head, 9) 
    head = push(head, 10) 
    head = push(head, 16) 
    head = push(head, 15) 
    k = 3
    product = productofnode(head, k) 
    print("product =", product) 
# this code is contributed by arnab kundu 

c

// c# program to find product of nodes in a  
// doubly linked list divisible by k  
using system; 
class gfg 
{ 
// doubly linked list node  
public class node 
{  
    public int data;  
    public node prev, next;  
};  
// function to insert a node at the beginning  
// of the doubly linked list  
static node push(node head_ref, int new_data)  
{  
    // allocate node  
    node new_node = new node();  
    // put in the data  
    new_node.data = new_data;  
    // since we are adding at the beginning,  
    // prev is always null  
    new_node.prev = null;  
    // link the old list off the new node  
    new_node.next = (head_ref);  
    // change prev of head node to new node  
    if ((head_ref) != null)  
        (head_ref).prev = new_node;  
    // move the head to point to the new node  
    (head_ref) = new_node;  
    return head_ref; 
}  
// function to find product of all the nodes from  
// the doubly linked list that are divisible by k  
static int productofnode(node head_ref, int k)  
{  
    node ptr = head_ref;  
    node next;  
    int product = 1;  
    // travese list till last node  
    while (ptr != null) 
    {  
        next = ptr.next;  
        // check is node value divided by k  
        // if true then add in sum  
        if (ptr.data % k == 0)  
            product *= ptr.data;  
        ptr = next;  
    }  
    // return product of nodes which  
    // are divisible by k  
    return product;  
}  
// driver code  
public static void main(string []args) 
{  
    // start with the empty list  
    node head = null;  
    // create the doubly linked list  
    // 15 16 10 9 6 7 17  
    head = push(head, 17);  
    head = push(head, 7);  
    head = push(head, 6);  
    head = push(head, 9);  
    head = push(head, 10);  
    head = push(head, 16);  
    head = push(head, 15);  
    int k = 3;  
    int prod = productofnode(head, k);  
    console.writeline( "product = "   prod);  
} 
} 
// this code contributed by rajput-ji 

输出

product = 810

时间复杂度o(n),其中n是节点数。



如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。