Skip to content

Commit

Permalink
Adding Insert at Starts of Singly Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
sadhusneha3 authored Oct 14, 2023
1 parent 9c1eeb8 commit ef87206
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
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 ef87206

Please sign in to comment.