Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Node{
- public:
- int val;
- Node * next;
- };
- Node * insert(Node * head , int x){
- Node * a = new Node();
- a->val = x;
- if(head==NULL){
- head = a;
- }
- else{
- a->next = head;
- head = a;
- }
- return head;
- }
- void printl(Node * head){
- Node * temp = head;
- while(temp!=NULL){
- cout<<temp->val<<" ";
- temp = temp->next;
- }
- }
- int main(){
- int n;
- Node * head= NULL;
- cout<<"Enter the size of your linkedlist : \n";
- cin>>n;
- for(int i=0; i<n; i++){
- cout<<"Enter a number : \n";
- int x; //3->2->1
- cin>>x;
- head = insert(head,x);
- }
- printl(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement