Posts

Easy Algorithms of Linked List

  1. Algorithm to Insert a Node at the Beginning 📌 Adds a new node at the start of the linked list. Algorithm: 1. Create a new node. 2. Assign data to the new node. 3. Make the new node point to the current head. 4. Update head to the new node. 5. End. ✅ C Code: #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void insertAtBeginning(struct Node** head, int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = *head; *head = newNode; } 2. Algorithm to Insert a Node at the End 📌 Adds a new node at the end of the linked list. Algorithm: 1. Create a new node. 2. Assign data to the new node. 3. If the list is empty, make the new node the head. 4. Otherwise, traverse to the last node. 5. Make the last node’s next point to the new node. 6. End. ✅ C Code: void insertAtEnd(struct Node** head, int value) { struct Node* newNode = (struct Node*)mallo...
Recent posts