
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the product of last N nodes of the given Linked List in C++
Consider we have few elements in a linked list. We have to find the multiplication result of last n number of elements. The value of n is also given. So if the list is like [5, 7, 3, 5, 6, 9], and n = 3, then result will be 5 * 6 * 9 = 270.
The process is straight forward. We simply read the current element starting from left side, then add the elements into stack. After filling up the stack, remove n elements and multiply them with the prod. (initially prod is 1), when n number of elements are traversed, then stop.
Example
#include<iostream> #include<stack> using namespace std; class Node{ public: int data; Node *next; }; Node* getNode(int data){ Node *newNode = new Node; newNode->data = data; newNode->next = NULL; return newNode; } void append(struct Node** start, int key) { Node* new_node = getNode(key); Node *p = (*start); if(p == NULL){ (*start) = new_node; return; } while(p->next != NULL){ p = p->next; } p->next = new_node; } long long prodLastNElements(Node *start, int n) { if(n <= 0) return 0; stack<int> stk; long long res = 1; Node* temp = start; while (temp != NULL) { stk.push(temp->data); temp = temp->next; } while(n--){ res *= stk.top(); stk.pop(); } return res; } int main() { Node *start = NULL; int arr[] = {5, 7, 3, 5, 6, 9}; int size = sizeof(arr)/sizeof(arr[0]); int n = 3; for(int i = 0; i<size; i++){ append(&start, arr[i]); } cout << "Product of last n elements: " << prodLastNElements(start, n); }
Output
Product of last n elements: 270
- Related Articles
- Find the product of first k nodes of the given Linked List in C++
- Product of the alternate nodes of linked list
- Product of the nodes of a Singly Linked List
- Delete N nodes after M nodes of a linked list in C++?
- Product of all prime nodes in a Singly Linked List in C++
- Delete N nodes after M nodes of a linked list in C++ program
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Print the last k nodes of the linked list in reverse order Recursive Approaches in C language
- Print the last k nodes of the linked list in reverse order Iterative approach in C language
- Print nodes of linked list at given indexes in C language
- Sum of the alternate nodes of linked list in C++
- Sum of the nodes of a Circular Linked List in C++
- Find the common nodes in two singly linked list in C++
- Sum of the nodes of a Singly Linked List in C Program

Advertisements