Product of the alternate nodes of linked list

CServer Side ProgrammingProgramming

Given with n nodes the task is to print the product of alternate node in a linked list. The program must only print the product of alternate nodes without actually changing the locations of the nodes.


Input -: 10 20 30 40 50 60
Output -: 15000

In the above example, starting from first node which is 10 alternate nodes are 10, 30, 50 and their product is 10*30*50 = 15000.

In the above diagram, blue coloured nodes are the alternate nodes, if we start from first node and red coloured nodes are non considerable nodes.

Approach used below is as follows

  • Take a temporary pointer, lets say, temp of type node

  • Set this temp pointer to first node which is pointed by head pointer

  • Move temp to temp ->next -> next while the situation (temp->next!=NULL && temp!=NULL && temp->next->next!=NULL) holds true

  • Set product=product*(temp->data)


Step 1 -> create structure of a node and temp, next and head as pointer to a structure node
   struct node
      int data
      struct node *next, *head, *temp
Step 2 -> declare function to insert a node in a list
   void insert(int val)
      struct node* newnode = (struct node*)malloc(sizeof(struct node))
      newnode->data = val
      IF head= NULL
         set head = newnode
         set head->next = NULL
         Set temp=head
         Loop While temp->next!=NULL
            Set temp=temp->next
         Set newnode->next=NULL
         Set temp->next=newnode
Step 3 -> Declare a function to display list
   void display()
      IF head=NULL
         Print no node
   Set temp=head
   Loop While temp!=NULL
      Print temp->data
      Set temp=temp->next
Step 4 -> declare a function to find alternate nodes
   void alternate()
      declare int product
      Set temp=head
      Set product=head->data
      Loop While(temp->next!=NULL && temp!=NULL && temp->next-
         Set temp=temp->next->next
         Set product=product * (temp->data)
      Print product
Step 5 -> in main()
   Create nodes using struct node* head = NULL;
   Call function insert(10) to insert a node
   Call display() to display the list
   Call alternate() to find alternate nodes product


 Live Demo

//structure of a node
struct node {
   int data;
   struct node *next;
//function for inserting nodes into a list
void insert(int val) {
   struct node* newnode = (struct node*)malloc(sizeof(struct node));
   newnode->data = val;
   if(head == NULL) {
      head = newnode;
      head->next = NULL;
      } else {
      while(temp->next!=NULL) {
//function for displaying a list
void display() {
      printf("no node ");
   else {
      while(temp!=NULL) {
         printf("%d ",temp->data);
//function for finding alternate elements
void alternate() {
   int product;
   while(temp->next!=NULL && temp!=NULL && temp->next->next!=NULL) {
      product=product * (temp->data);
   printf("\nproduct of alternate nodes is %d : " ,product);
int main() {
   //creating list
   struct node* head = NULL;
   //inserting elements into a list
   //displaying the list
   printf("linked list is : ");

   //calling alternate function for finding product
   return 0;


linked list is : 10 20 30 40 50 60
product of alternate nodes is : 15000
Published on 20-Sep-2019 11:16:32