InterviewSolution
| 1. |
Describe The Steps To Insert Data At The Starting Of A Singly Linked List? |
|
Answer» INSERTING data at the beginning of the LINKED list involves creation of a new node, inserting the new node by assigning the head pointer to the new node NEXT pointer and UPDATING the head pointer to the POINT the new node. Consider inserting a temp node to the first of list Node *head; void InsertNodeAtFront(int data) { /* 1. create the new node*/ Node *temp = new Node; temp->data = data; /* 2. insert it at the first position*/ temp->next = head; /* 3. update the head to point to this new node*/ head = temp; } Inserting data at the beginning of the linked list involves creation of a new node, inserting the new node by assigning the head pointer to the new node next pointer and updating the head pointer to the point the new node. Consider inserting a temp node to the first of list Node *head; void InsertNodeAtFront(int data) { /* 1. create the new node*/ Node *temp = new Node; temp->data = data; /* 2. insert it at the first position*/ temp->next = head; /* 3. update the head to point to this new node*/ head = temp; } |
|