problem no 28:(linked list),print the number of nodes in the linked list.
QUESTION:
Given a singly linked list. The task is to find the length of the linked list, where length is defined as the number of nodes in the linked list.
Example 1:
Input: LinkedList: 1->2->3->4->5 Output: 5 Explanation: Count of nodes in the linked list is 5, which is its length.
Example 2:
Input: LinkedList: 2->4->6->7->5->1->0 Output: 7 Explanation: Count of nodes in the linked list is 7. Hence, the output is 7.
ANSWER:
int getCount(struct Node* head){
int count=1;
struct Node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
count+=1;
}
return count;
}
Comments
Post a Comment