Sal Il Insertion

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

struct Node *insertAtBegining(struct Node *head, int x) {

struct Node* newPtr = (struct Node*)malloc(sizeof(struct Node));


newPtr->data = x;

if(!head) return newPtr;

newPtr->next = head;
head = newPtr;

return head;

//code here
}

//Function to insert a node at the end of the linked list.


struct Node *insertAtEnd(struct Node *head, int x) {

struct Node* newPtr = (struct Node*)malloc(sizeof(struct Node));


newPtr->data = x;

if(!head) return newPtr;

struct Node* cur = head;

while(cur->next)
{
cur = cur->next;
}

cur->next = newPtr;
return head;

You might also like