Find Length of a Linked List (Iterative)
1,298.0K 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-
How Many Sweets?
Sally and Anna bought 8 sweets and shared between themselves. Sally ate 2 more sweets than Anna did. How many ...Read More »Found underground riddle
These are a type of starchy food Mashed or fried they can be found They often come from Idaho And ...Read More »What am I?
If you look, you can’t see me. If you see me, you cannot see anything else. I can make you ...Read More »I’m very smart and can be dangerous riddle
I’m a 8 letter word let’s say 12345678. I’m very smart and can be dangerous, My 78 is the name ...Read More »Soldiers Parade Riddle
In Soldiers Parade, Captain Jacob gives the order to soldiers – Right Turn – About turn – Left turn Assuming ...Read More »Who was in the photograph?
A man is looking at a photograph of someone. His friend asks who it is. The man replies, “Brothers and ...Read More »Who is the doctor?
A boy and a doctor was fishing.The boy is the doctor’s son but the doctor is not the boy’s father. ...Read More »Four Letter Word riddle
Which four-letter word can be written forward, backward or upside down, and can still be read from left to right ...Read More »Key Hardest to Turn
Name a key which is hardest to turn.Read More »In which year was the grandfather born?
Recently, somebody told: “My grandfather was born on the first sunday of the year. His seventh birthday was also on ...Read More »What does this mean
What does this below simple rebus mean?Read More »Name 3 consecutive days puzzle
Can You Name Three Consecutive Days. Without Using The Words – Monday , Tuesday, Wednesday, Thursday, Friday, Saturday, Or Sunday?Read More »How long it will take?
It takes ten men ten hours to build a certain wall. How long does it take five men to build ...Read More »Five Brothers
My three brothers, my cousin and I are stood in a line. I am the shortest of all my brothers ...Read More »Teaching Year Riddle
Three teachers were discussing how long they had been teaching. Adrian and Betty had been teaching for a total of ...Read More »Count the Triangles
Can you count the triangle in below given figure ?Read More »Number Games
Fill in the boxes with numbers from 1-10. One number can be used only once.Read More »Which way leads to the gift
How did the man win the bet?
A boy at a carnival went to a booth ran by a man who said “If I can write your ...Read More »Which one weigh more?
Does an ounce of gold or an ounce of feathers weight moreRead 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