-
Notifications
You must be signed in to change notification settings - Fork 22
/
find fractional node
58 lines (48 loc) · 1.19 KB
/
find fractional node
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
/*
Given a singly linked list and a number k. Write a function to find the (N/k)th element, where N is the number of elements in the list. We
need to consider ceil value in case of decimals.
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case consists of an integer
N. The second line consists of N spaced integers.The last line consists of an integer k.
Output:
Print the data value of (N/k)th node in the Linked List.
User Task:
The task is to complete the function fractional_node() which should find the n/kth node in the linked list and return its data value.
Constraints:
1 <= T <= 100
1 <= N <= 100
Example:
Input:
2
6
1 2 3 4 5 6
2
5
2 7 9 3 5
3
Output:
3
7
Explanation:
Testcase 1: 6/2th element is the 3rd(1-based indexing) element which is 3.
*/
int fractional_node(struct Node *head, int k)
{
struct Node *t=head, *temp=head;
int len=0,i=1,ans=0;
while(t){
len++;
t=t->next;
}
int count = len/k;
if(len%k != 0) count++;
while(temp!=NULL){
if(i==count){
ans=temp->data;
break;
}
temp=temp->next;
i++;
}
return ans;
}