Find Length of a Linked List (Iterative)
1,444.2K Views
Answered
Write a C function to count number of nodes in a given singly linked list.
For example, the function should return 6 for linked list 1->3->1->4->2->1.
Best answer
ITERATIVE SOLUTION-
/* * File: main.cpp * Author: akash * * Created on 12 August, 2015, 4:57 PM */ #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; }; //void push(struct **node top_head,int dataToEnter); //int getCount(struct *node top_head); void push(struct node** top_head,int dataToEnter) { struct node* add_node = (struct node*) malloc(sizeof(struct node)); add_node->data = dataToEnter; add_node->next = (*top_head); (*top_head) = add_node; } int getCount(struct node* top_head){ if (top_head == NULL) return 0; // count is 1 + count of remaining list return 1 + getCount(top_head->next); } int main(){ struct node* head = NULL; push(&head, 10); push(&head, 20); push(&head, 30); push(&head, 40); push(&head, 20); push(&head, 30); printf("Nodes Count is %d", getCount(head)); return 0; }
Your Answer
More puzzles to try-
Roaring Call riddle
This old one runs forever but never moves at all. He has not lungs nor throat, but still has a ...Read More »Who is the killer? Murder in restaurant puzzle
Who killed the lady in the washroom?Read More »Fuel in Car Race Riddle
Before the start of the car race, John and Jacob have the same amount of fuel in their car. With ...Read More »Robbery Ridle
The Bank of Mexico is abducted by 5 masked strangers. One of the strangers asked the teller to give them ...Read More »5 temple 5 river riddle
There are sequence of 5 Church separated by 5 rivers, Sequence is Church, River, Church, River………… A man has to ...Read More »Mr. Mark and his dog’s name puzzle
Mr. Mark went to the market with his dog.. He rode on a horse to the market, Yet walked… The ...Read More »Student and Farmer riddle
What is the difference between a school boy studying and a farmer watching his cattle?Read More »What happened to Angel and Dory
Angel and Dory were found on the ground gasping for air surrounded by glass and water. The person who found ...Read More »1 Ball is defected of of 8 can you find in 2 weighs?
There are 8 balls. 7 of them weigh the same. 1 of them has a different weight, (you don’t know ...Read More »Who deserves your seat and why?
Test of a psychopath
A young woman is attending her mother’s funeral. While there, she meets a man she has never seen before and ...Read More »Find hidden words in Fishing Picture puzzle
Family is enjoying fishing, why don’t you enjoy by finding some hidden words in this family fishing puzzle. Find 6 ...Read More »Can’t Buy or Sell
What is greater than gold but cannot be bought. it can never be sold and can earn if its sought. ...Read More »King of Measurement
I am a king who measures things quite well. What am I?Read More »Romans Castle Brain Twister
Once upon a time, there was a castle on a square island. The entire island was surrounded by a 14m ...Read More »Bones in human body riddle
Which is bigger in quantity and how much: days in a year or bones in an adult human body ?Read More »Farmer getting across the river
A farmer needs to take a fox, a chicken, and a sack of grain across a river. The only way ...Read More »Good for health bad for waist
My family is known to be crazy; I am good when I am alone and when spread I am amazing; ...Read More »Rebus Picture Riddle
What does this rebus picture mean?Read More »How many would he have in the first field?
A farmer has three fields. One of them has 3 bundles of hay, another has 4, and the last has ...Read More »
Accenture Interview PuzzlesAdobe Interview PuzzlesAge RiddleAkbar Birbal PuzzlesAlgorithm PuzzlesAlphabet riddleAmazon Interview PuzzlesAnalytical MathematicsAptitude PuzzleBank PuzzlesBetting PuzzlesBrain TeasersCalendar PuzzlesCards PuzzlesChess Board PuzzleChess PuzzlesChristmas Puzzlecipher PuzzleCivil Services PuzzleClock time puzzleCognizant Interview PuzzlesCoins PuzzleComputational PuzzleconundrumCoronavirus PuzzleCoupondunia Interview PuzzleCritical Thinking Puzzledata analyticsData Structure Interview QuestionsDecode PuzzleDetective PuzzlesDice PuzzleDictionary Riddlesdifficult riddleEasy Math puzzlesEasy puzzlesEinstein puzzleEnglish RiddleFamily Tree Puzzlefamous puzzleFill in the blanks riddlesFinding Killer RiddlesFlipkart interview puzzlesFunny RiddlesGeneral KnowledgeGeographical PuzzleGoogle Code Jam 2014Google Interview PuzzlesGRE PuzzleHard Puzzle