Sample

Get Prepinsta Prime

Get all 200+ courses offered by Prepinsta

Never Miss an OffCampus Update

Get OffCampus Updates on Social Media from PrepInsta

Follow us on our Media Handles, we post out OffCampus drives on our Instagram, Telegram, Discord, Whatsdapp etc.

Get Hiring Updates
Amazon,Google,Delottie & 30+companies are hiring ! Get hiring Updates right in your inbox from PrepInsta

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get PrepInsta Prime Subscription

Get access to all the courses that PrepInsta offers, check the out below -

Companies

TCS, Cognizant, Delloite, Infosys, Wipro, CoCubes, KPMG, Amazone, ZS Associates, Accenture, Congnizant & other 50+ companies

Programming

Data Structures, Top 500 Codes, C, C++, Java Python & other 10+ subjects

Skills

Full Stack Web Development, Data Science, Machine Learning, AWS Cloud, & other 10+ skills and 20+ projects

OffCampus Updates

Never Miss OffCampus Updates

Get OffCampus Update from PrepInsta

Follow us on our Social Media Handles

PrepSAT-Jobathon

\
* indicates required
Enter your personal email address
XYZ
ABC
enter 10 digit mobile no.
Enter 10 digit Mobile number
/ / ( dd / mm / yyyy )
01/12/1990
ABC College of Engineering and Technology
For BCA/ MCA enter "NA"

Shubham Agarwal

PrepInsta Prime Subscriber

My Dream Came True

TCS Digital (7 LPA) | Accolite (11 LPA) | Amazon (12 LPA) | Visa (24 LPA)

Getting PrepInsta Prime subscription is without doubt the best decision that I took. I wasn't good at coding and I became a good enough coder to crack product based companies

PrepInsta was giving over 150+ courses under One subscription, it was no brainer for me to join. I completed C,C++, DSA, Competitive Coding, Aptitude, TCS NQT, TCS Digital and Machine Learning Courses.

Try It

// Complete Program for Insertion in a Linked List in C++
#include<iostream>
using namespace std;

class Node
{
    public:
    int val;
    Node *next;
};

void insertAtBeginning(Node** head, int val){
    
    // dynamically create memory for this newNode
    Node* newNode = new Node();
    
    newNode->val = val;
    newNode->next = *head;

    *head = newNode;
    
    cout << newNode->val << " inserted" << endl; 
}

void insertAtLast(Node** head, int val){
Node* newNode = new Node();
newNode->val = val; // Last node always pointst to NULL newNode->next = NULL; // If Linked List was empty if(*head==NULL){ *head = newNode; cout << newNode->val << " inserted" << endl; return; } Node* tempNode = *head; // reach to the last node of Linked List while(tempNode->next!=NULL) tempNode = tempNode->next; // assign last node's next to this newNode tempNode->next = newNode; cout << newNode->val << " inserted" << endl; } // required for insertAfterNthNode int getCurrSize(Node* node){ int size=0; while(node!=NULL){ node = node->next; size++; } return size; } void insertAfterNthNode(int n, int val, Node** head) { int size = getCurrSize(*head); // Negative Position Insertions not possible // Can't insert if position to insert is greater than size of Linked List if(n < 0 || n > size) cout << "Invalid position to insert";
if(n == 0){
insertAtBeginning(head, val); } else { Node* newNode = new Node(); newNode->val = val; newNode->next = NULL; // tempNode used to traverse the Linked List Node* tempNode = *head; // traverse till the nth node while(--n) tempNode = tempNode->next; // assign newNode's next to nth node's next newNode->next= tempNode->next; // assign nth node's next to this new node tempNode->next = newNode; // newNode inserted cout << newNode->val << " inserted" << endl; } } void display(Node* node){ cout << "\n"; // as linked list will end when Node is Null while(node!=NULL){ cout << node->val << " ";
node = node->next; } cout << "\n" << endl; } int main() { Node* head = NULL; insertAtBeginning(&head,12); insertAtBeginning(&head,11); insertAtBeginning(&head,10); display(head); insertAtLast(&head,13); insertAtLast(&head,14); insertAtLast(&head,16); display(head); //Inserts data: 15 after 5th node insertAfterNthNode(5, 15,&head); insertAfterNthNode(0, 9,&head); display(head); return 0; }