给定一个包含N 个节点的单向链表,任务是从列表中找到其数据值为偶数和的所有节点的和和乘积。
例子:
Input: 15 -> 16 -> 8 -> 6 -> 13
Output:
Sum = 42
Product = 9360
Explanation:
The sum of all digit of number in linked list are:
15 = 1 + 5 = 6
16 = 1 + 6 = 7
8 = 8
6 = 6
13 = 1 + 3 = 4
The list contains 4 Even Digit Sum data values 15, 8, 6 and 13.
Sum = 15 + 8 + 6 + 13 = 42
Product = 15 * 8 * 6 * 13 = 9360
Input: 5 -> 3 -> 4 -> 2 -> 9
Output:
Sum = 6
Product = 8
Explanation:
The list contains 2 Even Digit Sum data values 4 and 2.
Sum = 4 + 2 = 6
Product = 4 * 2 = 8
方法:思想是遍历给定的链表,检查当前节点值的所有数字之和是否为偶数。如果是,则将当前节点值包括到结果总和和结果乘积中,否则检查下一个节点值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Node of Linked List
struct Node {
int data;
Node* next;
};
// Function to insert a node at the
// beginning of the singly Linked List
void push(Node** head_ref, int new_data)
{
// Allocate new node
Node* new_node
= (Node*)malloc(
sizeof(struct Node));
// Insert the data
new_node->data = new_data;
// Link old list to the new node
new_node->next = (*head_ref);
// Move head to point the new node
(*head_ref) = new_node;
}
// Function to find the digit sum
// for a number
int digitSum(int num)
{
int sum = 0;
while (num) {
sum += (num % 10);
num /= 10;
}
// Return the sum
return sum;
}
// Function to find the required
// sum and product
void sumAndProduct(Node* head_ref)
{
// Initialise the sum and product
// to 0 and 1 respectively
int prod = 1;
int sum = 0;
Node* ptr = head_ref;
// Traverse the given linked list
while (ptr != NULL) {
// If current node has even
// digit sum then include it in
// resultant sum and product
if (!(digitSum(ptr->data) & 1)) {
// Find the sum and the product
prod *= ptr->data;
sum += ptr->data;
}
ptr = ptr->next;
}
// Print the final Sum and Product
cout << "Sum = " << sum << endl;
cout << "Product = " << prod;
}
// Driver Code
int main()
{
// Head of the linked list
Node* head = NULL;
// Create the linked list
// 15 -> 16 -> 8 -> 6 -> 13
push(&head, 13);
push(&head, 6);
push(&head, 8);
push(&head, 16);
push(&head, 15);
// Function call
sumAndProduct(head);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Node of Linked List
static class Node {
int data;
Node next;
};
// Function to insert a node at the
// beginning of the singly Linked List
static Node push(Node head_ref, int new_data)
{
// Allocate new node
Node new_node
= new Node();
// Insert the data
new_node.data = new_data;
// Link old list to the new node
new_node.next = head_ref;
// Move head to point the new node
head_ref = new_node;
return head_ref;
}
// Function to find the digit sum
// for a number
static int digitSum(int num)
{
int sum = 0;
while (num > 0) {
sum += (num % 10);
num /= 10;
}
// Return the sum
return sum;
}
// Function to find the required
// sum and product
static void sumAndProduct(Node head_ref)
{
// Initialise the sum and product
// to 0 and 1 respectively
int prod = 1;
int sum = 0;
Node ptr = head_ref;
// Traverse the given linked list
while (ptr != null) {
// If current node has even
// digit sum then include it in
// resultant sum and product
if ((digitSum(ptr.data) %2 != 1)) {
// Find the sum and the product
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
// Print the final Sum and Product
System.out.print("Sum = " + sum +"\n");
System.out.print("Product = " + prod);
}
// Driver Code
public static void main(String[] args)
{
// Head of the linked list
Node head = null;
// Create the linked list
// 15.16.8.6.13
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
// Function call
sumAndProduct(head);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Node of Linked List
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Function to insert a node at the
# beginning of the singly Linked List
def push(head_ref, new_data):
# Insert the data
new_node = Node(new_data)
# Link old list to the new node
new_node.next = head_ref
# Move head to pothe new node
head_ref = new_node
return head_ref
# Function to find the digit sum
# for a number
def digitSum(num):
sum = 0
while (num):
sum += (num % 10)
num //= 10
# Return the sum
return sum
# Function to find the required
# sum and product
def sumAndProduct(head_ref):
# Initialise the sum and product
# to 0 and 1 respectively
prod = 1
sum = 0
ptr = head_ref
# Traverse the given linked list
while (ptr != None):
# If current node has even
# digit sum then include it in
# resultant sum and product
if (not (digitSum(ptr.data) & 1)):
# Find the sum and the product
prod *= ptr.data
sum += ptr.data
ptr = ptr.next
# Print the final Sum and Product
print("Sum =", sum)
print("Product =", prod)
# Driver Code
if __name__ == '__main__':
# Head of the linked list
head = None
# Create the linked list
# 15 . 16 . 8 . 6 . 13
head = push(head, 13)
head = push(head, 6)
head = push(head, 8)
head = push(head, 16)
head = push(head, 15)
# Function call
sumAndProduct(head)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Node of Linked List
class Node
{
public int data;
public Node next;
};
// Function to insert a node at the
// beginning of the singly Linked List
static Node push(Node head_ref, int new_data)
{
// Allocate new node
Node new_node = new Node();
// Insert the data
new_node.data = new_data;
// Link old list to the new node
new_node.next = head_ref;
// Move head to point the new node
head_ref = new_node;
return head_ref;
}
// Function to find the digit sum
// for a number
static int digitSum(int num)
{
int sum = 0;
while (num > 0)
{
sum += (num % 10);
num /= 10;
}
// Return the sum
return sum;
}
// Function to find the required
// sum and product
static void sumAndProduct(Node head_ref)
{
// Initialise the sum and product
// to 0 and 1 respectively
int prod = 1;
int sum = 0;
Node ptr = head_ref;
// Traverse the given linked list
while (ptr != null)
{
// If current node has even
// digit sum then include it in
// resultant sum and product
if ((digitSum(ptr.data) % 2 != 1))
{
// Find the sum and the product
prod *= ptr.data;
sum += ptr.data;
}
ptr = ptr.next;
}
// Print the readonly Sum and Product
Console.Write("Sum = " + sum + "\n");
Console.Write("Product = " + prod);
}
// Driver Code
public static void Main(String[] args)
{
// Head of the linked list
Node head = null;
// Create the linked list
// 15.16.8.6.13
head = push(head, 13);
head = push(head, 6);
head = push(head, 8);
head = push(head, 16);
head = push(head, 15);
// Function call
sumAndProduct(head);
}
}
// This code is contributed by Rohit_ranjan
Javascript
Sum = 42
Product = 9360
时间复杂度: O(N) ,其中 N 是链表中的节点数。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live