Find the product of last N nodes of the given Linked List
Last Updated :
06 Sep, 2022
Given a linked list and a number N. Find the product of last n nodes of the linked list.
Constraints : 0 <= N <= number of nodes in the linked list.
Examples:
Input : List = 10->6->8->4->12, N = 2
Output : 48
Explanation : Product of last two nodes:
12 * 4 = 48
Input : List = 15->7->9->5->16->14, N = 4
Output : 10080
Explanation : Product of last four nodes:
9 * 5 * 16 * 14 = 10080
Method 1:(Iterative approach using user-defined stack)
Traverse the nodes from left to right. While traversing push the nodes to a user-defined stack. Then pops the top n values from the stack and find their product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int productOfLastN_NodesUtil( struct Node* head, int n)
{
if (n <= 0)
return 0;
stack< int > st;
int prod = 1;
while (head != NULL) {
st.push(head->data);
head = head->next;
}
while (n--) {
prod *= st.top();
st.pop();
}
return prod;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 4);
push(&head, 8);
push(&head, 6);
push(&head, 10);
int n = 2;
cout << productOfLastN_NodesUtil(head, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node head;
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
head = head_ref;
}
static int productOfLastN_NodesUtil(Node head,
int n)
{
if (n <= 0 )
return 0 ;
Stack<Integer> st = new Stack<Integer>();
int prod = 1 ;
while (head != null )
{
st.push(head.data);
head = head.next;
}
while (n-- > 0 )
{
prod *= st.peek();
st.pop();
}
return prod;
}
public static void main(String[] args)
{
head = null ;
push(head, 12 );
push(head, 4 );
push(head, 8 );
push(head, 6 );
push(head, 10 );
int n = 2 ;
System.out.println(productOfLastN_NodesUtil(head, n));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = next
head = None
def push(head_ref, new_data):
global head
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
head = head_ref
def productOfLastN_NodesUtil(head, n):
if (n < = 0 ):
return 0
st = []
prod = 1
while (head ! = None ) :
st.append(head.data)
head = head. next
while (n > 0 ) :
n = n - 1
prod * = st[ - 1 ]
st.pop()
return prod
head = None
push(head, 12 )
push(head, 4 )
push(head, 8 )
push(head, 6 )
push(head, 10 )
n = 2
print (productOfLastN_NodesUtil(head, n))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int productOfLastN_NodesUtil(Node head,
int n)
{
if (n <= 0)
return 0;
int prod = 1, len = 0;
Node temp = head;
while (temp != null )
{
len++;
temp = temp.next;
}
int c = len - n;
temp = head;
while (temp != null && c-- >0)
temp = temp.next;
while (temp != null )
{
prod *= temp.data;
temp = temp.next;
}
return prod;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
int n = 2;
Console.Write(productOfLastN_NodesUtil(head, n));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
head = head_ref;
}
function productOfLastN_NodesUtil(head , n) {
if (n <= 0)
return 0;
var st = [];
var prod = 1;
while (head != null ) {
st.push(head.data);
head = head.next;
}
while (n-- > 0) {
prod *= st.pop();
}
return prod;
}
head = null ;
push(head, 12);
push(head, 4);
push(head, 8);
push(head, 6);
push(head, 10);
var n = 2;
document.write(productOfLastN_NodesUtil(head, n));
</script>
|
Time complexity : O(n)
Method 2: (Recursive approach using system call stack)
Recursively traverse the linked list up to the end. Now during the return from the function calls, multiply the last n nodes. The product can be accumulated in some variable passed by reference to the function or to some global variable.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void productOfLastN_Nodes( struct Node* head, int * n,
int * prod)
{
if (!head)
return ;
productOfLastN_Nodes(head->next, n, prod);
if (*n > 0) {
*prod = *prod * head->data;
--*n;
}
}
int productOfLastN_NodesUtil( struct Node* head, int n)
{
if (n <= 0)
return 0;
int prod = 1;
productOfLastN_Nodes(head, &n, &prod);
return prod;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 4);
push(&head, 8);
push(&head, 6);
push(&head, 10);
int n = 2;
cout << productOfLastN_NodesUtil(head, n);
return 0;
}
|
Java
class GFG{
static int n, prod;
static class Node {
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void productOfLastN_Nodes(Node head)
{
if (head== null )
return ;
productOfLastN_Nodes(head.next);
if (n > 0 ) {
prod = prod * head.data;
--n;
}
}
static int productOfLastN_NodesUtil(Node head)
{
if (n <= 0 )
return 0 ;
prod = 1 ;
productOfLastN_Nodes(head);
return prod;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 12 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 6 );
head = push(head, 10 );
n = 2 ;
System.out.print(productOfLastN_NodesUtil(head));
}
}
|
Python3
n, prod = 0 , 0 ;
class Node:
def __init__( self , data):
self .data = data
self . next = next
def push(head_ref, new_data):
new_Node = Node( 0 );
new_Node.data = new_data;
new_Node. next = head_ref;
head_ref = new_Node;
return head_ref;
def productOfLastN_Nodes(head):
global n, prod;
if (head = = None ):
return ;
productOfLastN_Nodes(head. next );
if (n > 0 ):
prod = prod * head.data;
n - = 1 ;
def productOfLastN_NodesUtil(head):
global n,prod;
if (n < = 0 ):
return 0 ;
prod = 1 ;
productOfLastN_Nodes(head);
return prod;
if __name__ = = '__main__' :
head = None ;
n = 2 ;
head = push(head, 12 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 6 );
head = push(head, 10 );
print (productOfLastN_NodesUtil(head));
|
C#
using System;
public class GFG{
static int n, prod;
public class Node {
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void productOfLastN_Nodes(Node head)
{
if (head== null )
return ;
productOfLastN_Nodes(head.next);
if (n > 0) {
prod = prod * head.data;
--n;
}
}
static int productOfLastN_NodesUtil(Node head)
{
if (n <= 0)
return 0;
prod = 1;
productOfLastN_Nodes(head);
return prod;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
n = 2;
Console.Write(productOfLastN_NodesUtil(head));
}
}
|
Javascript
<script>
var n, prod;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function productOfLastN_Nodes(head) {
if (head == null )
return ;
productOfLastN_Nodes(head.next);
if (n > 0) {
prod = prod * head.data;
--n;
}
}
function productOfLastN_NodesUtil(head) {
if (n <= 0)
return 0;
prod = 1;
productOfLastN_Nodes(head);
return prod;
}
var head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
n = 2;
document.write(productOfLastN_NodesUtil(head));
</script>
|
Time complexity: O(n)
Method 3 (Reversing the linked list):
Following are the steps:
- Reverse the given linked list.
- Traverse the first n nodes of the reversed linked list.
- While traversing multiply them.
- Reverse the linked list back to its original order.
- Return the product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void reverseList( struct Node** head_ref)
{
struct Node *current, *prev, *next;
current = *head_ref;
prev = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
int productOfLastN_NodesUtil( struct Node* head, int n)
{
if (n <= 0)
return 0;
reverseList(&head);
int prod = 1;
struct Node* current = head;
while (current != NULL && n--) {
prod *= current->data;
current = current->next;
}
reverseList(&head);
return prod;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 4);
push(&head, 8);
push(&head, 6);
push(&head, 10);
int n = 2;
cout << productOfLastN_NodesUtil(head, n);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static Node reverseList(Node head_ref)
{
Node current, prev, next;
current = head_ref;
prev = null ;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
return head_ref;
}
static int productOfLastN_NodesUtil(Node head, int n)
{
if (n <= 0 )
return 0 ;
head = reverseList(head);
int prod = 1 ;
Node current = head;
while (current != null && n-- > 0 )
{
prod *= current.data;
current = current.next;
}
head = reverseList(head);
return prod;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 12 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 6 );
head = push(head, 10 );
int n = 2 ;
System.out.print(productOfLastN_NodesUtil(head, n));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data);
new_node.data = new_data;
new_node. next = (head_ref);
(head_ref) = new_node;
return head_ref
def reverseList(head_ref):
next = None
current = head_ref;
prev = None ;
while (current ! = None ):
next = current. next ;
current. next = prev;
prev = current;
current = next ;
head_ref = prev
return head_ref
def productOfLastN_NodesUtil(head, n):
if (n < = 0 ):
return 0 ;
head = reverseList(head);
prod = 1 ;
current = head;
while (current ! = None and n):
n - = 1
prod * = current.data;
current = current. next ;
head = reverseList(head);
return prod;
if __name__ = = '__main__' :
head = None ;
head = push(head, 12 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 6 );
head = push(head, 10 );
n = 2 ;
print (productOfLastN_NodesUtil(head, n))
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static Node reverseList(Node head_ref)
{
Node current, prev, next;
current = head_ref;
prev = null ;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
return head_ref;
}
static int productOfLastN_NodesUtil(Node head, int n)
{
if (n <= 0)
return 0;
head = reverseList(head);
int prod = 1;
Node current = head;
while (current != null && n-- >0)
{
prod *= current.data;
current = current.next;
}
head = reverseList(head);
return prod;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
int n = 2;
Console.Write(productOfLastN_NodesUtil(head, n));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function reverseList(head_ref) {
var current, prev, next;
current = head_ref;
prev = null ;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head_ref = prev;
return head_ref;
}
function productOfLastN_NodesUtil(head , n) {
if (n <= 0)
return 0;
head = reverseList(head);
var prod = 1;
var current = head;
while (current != null && n-- > 0) {
prod *= current.data;
current = current.next;
}
head = reverseList(head);
return prod;
}
var head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
var n = 2;
document.write(productOfLastN_NodesUtil(head, n));
</script>
|
Time complexity: O(n)
Method 4 (Using a length of linked list):
Following are the steps:
- Calculate the length of the given Linked List. Let it be len.
- First traverse the (len – n) nodes from the beginning.
- Then traverse the remaining n nodes and while traversing product them.
- Return the product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int productOfLastN_NodesUtil( struct Node* head, int n)
{
if (n <= 0)
return 0;
int prod = 1, len = 0;
struct Node* temp = head;
while (temp != NULL) {
len++;
temp = temp->next;
}
int c = len - n;
temp = head;
while (temp != NULL && c--)
temp = temp->next;
while (temp != NULL) {
prod *= temp->data;
temp = temp->next;
}
return prod;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 4);
push(&head, 8);
push(&head, 6);
push(&head, 10);
int n = 2;
cout << productOfLastN_NodesUtil(head, n);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int productOfLastN_NodesUtil(Node head, int n)
{
if (n <= 0 )
return 0 ;
int prod = 1 , len = 0 ;
Node temp = head;
while (temp != null )
{
len++;
temp = temp.next;
}
int c = len - n;
temp = head;
while (temp != null && c-- > 0 )
temp = temp.next;
while (temp != null )
{
prod *= temp.data;
temp = temp.next;
}
return prod;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 12 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 6 );
head = push(head, 10 );
int n = 2 ;
System.out.print(productOfLastN_NodesUtil(head, n));
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def push(head_ref, new_data):
new_node = Node();
new_node.data = new_data;
new_node. next = head_ref;
head_ref = new_node;
return head_ref;
def productOfLastN_NodesUtil(head, n):
if (n < = 0 ):
return 0 ;
prod = 1
len = 0 ;
temp = head;
while (temp ! = None ):
len + = 1
temp = temp. next ;
c = len - n;
temp = head;
while (temp ! = None and c > 0 ):
c - = 1
temp = temp. next ;
while (temp ! = None ):
prod * = temp.data;
temp = temp. next ;
return prod;
if __name__ = = '__main__' :
head = None ;
head = push(head, 12 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 6 );
head = push(head, 10 );
n = 2 ;
print (productOfLastN_NodesUtil(head, n));
|
C#
using System;
public class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int productOfLastN_NodesUtil(Node head, int n)
{
if (n <= 0)
return 0;
int prod = 1, len = 0;
Node temp = head;
while (temp != null )
{
len++;
temp = temp.next;
}
int c = len - n;
temp = head;
while (temp != null && c-- >0)
temp = temp.next;
while (temp != null )
{
prod *= temp.data;
temp = temp.next;
}
return prod;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
int n = 2;
Console.Write(productOfLastN_NodesUtil(head, n));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function productOfLastN_NodesUtil(head, n)
{
if (n <= 0)
return 0;
var prod = 1, len = 0;
var temp = head;
while (temp != null )
{
len++;
temp = temp.next;
}
var c = len - n;
temp = head;
while (temp != null && c-- >0)
temp = temp.next;
while (temp != null )
{
prod *= temp.data;
temp = temp.next;
}
return prod;
}
var head = null ;
head = push(head, 12);
head = push(head, 4);
head = push(head, 8);
head = push(head, 6);
head = push(head, 10);
var n = 2;
document.write(productOfLastN_NodesUtil(head, n));
</script>
|
Time complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...