-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path25 Recursion_ Find Middle of List.cpp
More file actions
92 lines (77 loc) · 1.69 KB
/
25 Recursion_ Find Middle of List.cpp
File metadata and controls
92 lines (77 loc) · 1.69 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
given a singly linked list, find the middle of the linked list. For example, if the given linked list is A->B->C->D->E then the output should be C. If there are even nodes, then there would be two middle nodes, we need to print the first middle element. For example, if the given linked list is A->B->C->D->E->F then the output should be C.
Input Format
first line reads the number of elements N and
second line reads N elements of linked list
Example
5
A B C D E
Constraints
1<= N <= 20
Output Format
Prints the middle element of the linked list
Example:
C
Sample Input 0
5
A B C D E
Sample Output 0
C
*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct llist
{
char data;
llist *next=NULL;
};
void middle(llist *slow, llist *fast)
{
if(slow==NULL)
{
return;
}
if(fast->next==NULL)
{
cout<<slow->data;
return;
}
if(fast->next->next==NULL)
{
cout<<slow->data;
return;
}
if(slow!=NULL || fast->next!=NULL || fast->next->next!=NULL)
{
middle(slow->next,fast->next->next);
}
}
llist *head=NULL;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;//number of elements
cin>>n;
llist *temp=NULL;
for(int i=0;i<n;i++)//for input of n elements
{
if(head==NULL)//first node
{
head = new llist;
cin>>head->data;
temp = head;
}
else
{
llist *p = new llist;
cin>>p->data;
temp->next=p;
temp = p;
}
}
middle(head,head);
return 0;
}