-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_nth_element.cpp
More file actions
62 lines (53 loc) · 1.29 KB
/
get_nth_element.cpp
File metadata and controls
62 lines (53 loc) · 1.29 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
#include<bits/stdc++.h>
using namespace std;
/* Link list node */
struct node
{
int data;
struct node* next;
};
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;
}
int GetNth(struct node* head, int index);
/* Driver program to test above function*/
int main(){
int T,i,n,l,k;
cin>>T;
while(T--){
struct node *head = NULL;
cin>>n>>k;
for(i=1;i<=n;i++)
{
cin>>l;
push(&head,l);
}
cout<<GetNth(head, n-k);
}
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.*/
/* Print he nth node in the linked list Node is defined as
struct node
{
int data;
struct node* next;
}; */
// Should return data of node at given index. The function may
// assume that there are at least index+1 nodes in linked list
int GetNth(struct node* head, int index){
node* temp = head;
while(temp && temp->next && index){
temp=temp->next;
index--;
}
return temp->data;
// Code here
}