forked from fedeoliv/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_node.cpp
More file actions
51 lines (46 loc) · 814 Bytes
/
Copy pathinsert_node.cpp
File metadata and controls
51 lines (46 loc) · 814 Bytes
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
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node
{
int data;
Node *next;
};/*
Insert Node at the end of a linked list
head pointer input could be NULL as well for empty list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Insert(Node *head,int data)
{
Node *node = new Node();
node->data = data;
node->next = NULL;
if(head == NULL) return node;
Node *temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = node;
return head;
}void Print(Node *head)
{
Node *temp = head;
while(temp!= NULL){ cout<<temp->data<<"\n";temp = temp->next;}
}
int main()
{
int t;
cin>>t;
Node *head = NULL;
while(t-- >0)
{
int x; cin>>x;
head = Insert(head,x);
}
Print(head);
}