-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 15: Linked List
58 lines (56 loc) · 1 KB
/
Day 15: Linked List
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
#include <iostream>
#include <cstddef>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node(int d){
data=d;
next=NULL;
}
};
class Solution{
public:
// Node* strt = NULL;
Node* end =NULL;
Node *insert(Node *head, int data)
{
Node *temp = (Node *)malloc(sizeof(Node));
temp->next = NULL;
temp->data = data;
if (head == NULL)
{
head=temp;
end= head;
}
else
{
end->next = temp;
end = temp;
}
return head;
}
void display(Node *head)
{
Node *start=head;
while(start)
{
cout<<start->data<<" ";
start=start->next;
}
}
};
int main()
{
Node* head=NULL;
Solution mylist;
int T,data;
cin>>T;
while(T-->0){
cin>>data;
head=mylist.insert(head,data);
}
mylist.display(head);
}