Skip to content

Commit 07c9287

Browse files
Day 15 Linked List
1 parent 9f9ce3f commit 07c9287

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

Hackerrank/c++/Day_15_Linked_List.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <cstddef>
3+
using namespace std;
4+
class Node
5+
{
6+
public:
7+
int data;
8+
Node *next;
9+
Node(int d){
10+
data=d;
11+
next=NULL;
12+
}
13+
};
14+
class Solution{
15+
public:
16+
17+
Node* insert(Node *head,int data)
18+
{
19+
//Complete this method
20+
Node* newNode = new Node(data);
21+
Node* tail = head;
22+
23+
if(head == NULL)
24+
{
25+
return newNode;
26+
}
27+
else
28+
{
29+
for (;tail->next ; tail = tail->next);
30+
tail->next = newNode;
31+
return head;
32+
}
33+
}
34+
35+
void display(Node *head)
36+
{
37+
Node *start=head;
38+
while(start)
39+
{
40+
cout<<start->data<<" ";
41+
start=start->next;
42+
}
43+
}
44+
};
45+
int main()
46+
{
47+
#ifndef ONLINE_JUDGE
48+
freopen("input.txt","r",stdin);
49+
freopen("output.txt","w",stdout);
50+
#endif
51+
52+
Node* head=NULL;
53+
Solution mylist;
54+
int T,data;
55+
cin>>T;
56+
while(T-->0){
57+
cin>>data;
58+
head=mylist.insert(head,data);
59+
}
60+
mylist.display(head);
61+
62+
}

0 commit comments

Comments
 (0)