-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkthmiddlenode.cpp
More file actions
57 lines (55 loc) · 1.03 KB
/
kthmiddlenode.cpp
File metadata and controls
57 lines (55 loc) · 1.03 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
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node(int val){
data=val;
next=NULL;
}
};
class list{
node* head;
public:
list(){head=NULL;}
void insert(int ins){
node* t = new node(ins);
if(head==NULL){
head=t;
return;
}
node* temp =head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = t;
}
int middle(){
node* slow=head;
node* fast=head;
while(fast!=NULL && fast->next!=NULL ){
slow=slow->next;
fast=fast->next->next;
}
return slow->data;
}
void printList() {
node* temp = head;
while (temp) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
};
int main(){
list l;
l.insert(10);
l.insert(20);
l.insert(30);
l.insert(50);
l.printList();
l.middle();
l.printList();
}