给定一个指向单链表头的指针和一个整数k 。任务是找到链表中前k个节点的乘积。
例子:
Input: 10 -> 6 -> 8 -> 4 -> 12, k = 2
Output: 60
10 * 6 = 60
Input: 15 -> 7 -> 9 -> 5 -> 16 -> 14, k = 4
Output: 4725
15 * 7 * 9 * 5 = 4725
方法:设置prod = 1 (必需产品),计数= 0 (遍历的节点数)。现在,开始从左到右遍历链表的节点,并在count
下面是上述方法的实现:
C++
// C++ program to find the product of first
// 'k' nodes of the Linked List
#include
using namespace std;
#define ll long long int
/* A Linked list node */
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node;
/* put in the data */
new_node->data = new_data;
/* link the old list to the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// Function to return the product of
// first k nodes of the given linked list
ll product(struct Node* head, int k)
{
if (k <= 0)
return 0;
ll prod = 1;
int i = 0;
Node* node = head;
// Traverse the list from left to right
while (i < k) {
// Update product
prod = prod * node->data;
// Move to the next node
node = node->next;
i++;
}
// Return the required product
return prod;
}
// Driver code
int main()
{
struct Node* head = NULL;
// Linked list 10 -> 6 -> 8 -> 4 -> 12
push(&head, 12);
push(&head, 4);
push(&head, 8);
push(&head, 6);
push(&head, 10);
int k = 2;
cout << product(head, k);
return 0;
}
Java
// Java program to find the product of first
// 'k' nodes of the Linked List
class Solution
{
/* A Linked list node */
static class Node
{
int data;
Node next;
}
// Function to insert a node at the
// beginning of the 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;
/* link the old list to the new node */
new_node.next = (head_ref);
/* move the head to point to the new node */
(head_ref) = new_node;
return head_ref;
}
// Function to return the product of
// first k nodes of the given linked list
static long product( Node head, int k)
{
if (k <= 0)
return 0;
long prod = 1;
int i = 0;
Node node = head;
// Traverse the list from left to right
while (i < k)
{
// Update product
prod = prod * node.data;
// Move to the next node
node = node.next;
i++;
}
// Return the required product
return prod;
}
// Driver code
public static void main(String args[])
{
Node head = new Node();
// Linked list 10 . 6 . 8 . 4 . 12
head=push(head, 12);
head=push(head, 4);
head=push(head, 8);
head=push(head, 6);
head=push(head, 10);
int k = 2;
System.out.println( product(head, k));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find the product
# of first 'k' nodes of the Linked List
import math
#define ll long long
# A Linked list node
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to insert a node at the
# beginning of the linked list
def push(head_ref, new_data):
# allocate node
new_node = Node(new_data)
# put in the data
new_node.data = new_data
# link the old list to the new node
new_node.next = head_ref
# move the head to po to the new node
head_ref = new_node
return head_ref
# Function to return the product of
# first k nodes of the given linked list
def product(head, k):
if (k <= 0):
return 0
prod = 1
i = 0
node = head
# Traverse the list from left to right
while (i < k):
# Update product
prod = prod * node.data
# Move to the next node
node = node.next
i = i + 1
# Return the required product
return prod
# Driver code
if __name__=='__main__':
head = None
# Linked list 10 . 6 . 8 . 4 . 12
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
k = 2
print(product(head, k))
# This code is contributed by Srathore
C#
// C# program to find the product of first
// 'k' nodes of the Linked List
using System;
class GFG
{
/* A Linked list node */
class Node
{
public int data;
public Node next;
}
// Function to insert a node at the
// beginning of the 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;
/* link the old list to the new node */
new_node.next = (head_ref);
/* move the head to point to the new node */
(head_ref) = new_node;
return head_ref;
}
// Function to return the product of
// first k nodes of the given linked list
static long product( Node head, int k)
{
if (k <= 0)
return 0;
long prod = 1;
int i = 0;
Node node = head;
// Traverse the list from left to right
while (i < k)
{
// Update product
prod = prod * node.data;
// Move to the next node
node = node.next;
i++;
}
// Return the required product
return prod;
}
// Driver code
public static void Main()
{
Node head = new Node();
// Linked list 10 . 6 . 8 . 4 . 12
head=push(head, 12);
head=push(head, 4);
head=push(head, 8);
head=push(head, 6);
head=push(head, 10);
int k = 2;
Console.WriteLine( product(head, k));
}
}
// This code is contributed by PrinciRaj1992
输出:
60
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。