-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlength_of_LL.cpp
More file actions
64 lines (56 loc) · 1.28 KB
/
length_of_LL.cpp
File metadata and controls
64 lines (56 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data){
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
/* Counts no. of nodes in linked list */
int getCount(struct node* head);
int main(){
int T,i,n,l;
scanf("%d",&T);
while(T--){
struct node *head = NULL;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&l);
push(&head,l);
}
/* Check the count function */
printf("%d",getCount(head));
}
return 0;
}
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
/*
Count the number of nodes
in the linked list
Node is defined as
struct node
{
int data;
struct node* next;
};
*/
int getCount(struct node* head){
struct node* temp = head;
int len=0;
while(temp){
len++;
temp=temp->next;
}
return len;
//Code here
}