-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
48 lines (41 loc) · 822 Bytes
/
linked_list.cpp
File metadata and controls
48 lines (41 loc) · 822 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
#include<bits/stdc++.h>
using namespace std;
// Linked List Implementation using structure
// Node of LL
struct Node{
int data;
Node* next;
};
void push(Node* &head, int data){
Node* nn = new Node; // Created a New Node
nn->data = data;
nn->next = head;
head = nn;
}
Node* construct(vector<int>arr,int n){
Node* head = nullptr;
for(int i=n-1;i>=0;i--){
push(head,arr[i]);
}
return head;
}
void printLL(Node* head){
Node* temp=head;
while(temp!=nullptr){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
return;
}
main(){
int n,i,j;
cin>>n;
vector<int>arr(n);
for(i=0;i<n;i++)
cin>>arr[i];
// Construct LL and return head
Node* head = construct(arr,n);
// Print LL
printLL(head);
}