Skip to content

Commit

Permalink
Merge pull request #3 from sadhusneha3/main
Browse files Browse the repository at this point in the history
Insert at Start of Singly Linked List using C++
  • Loading branch information
AnkitMajee authored Oct 14, 2023
2 parents 8654fe1 + 8a6221b commit 8959d5c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Linked List/Insert at Starts of Singly Linked List using C++
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
I/P: 10->20->30->40 element=5
O/P: 5->10->20->30->40

I/P: Null element=50
O/P: 50

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insertAtBegin(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = head;
head = newNode;
}

void printList() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
}

int main() {
insertAtBegin(1);
insertAtBegin(2);
insertAtBegin(3);
insertAtBegin(4);
insertAtBegin(5);
cout << "Linked List: ";
printList();
return 0;
}

0 comments on commit 8959d5c

Please sign in to comment.