Hacker Rank Problem - Insert a node at the head of a linked list in c++
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson. You’re given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer, insert this node at the head of the linked list and return the new head node. The head pointer given may be null meaning that the initial list is empty.
Input Format You have to complete the SinglyLinkedListNode Insert(SinglyLinkedListNode head, int data) method which takes two arguments - the head of the linked list and the integer to insert. You should NOT read any input from stdin/console. The input is handled by code in the editor and is as follows: The first line contains an integer , denoting the number of elements to be inserted at the head of the list. The next lines contain an integer each, denoting the element to be inserted.
Constraints
Output Format Insert the new node at the head and return the head of the updated linked list. Do NOT print anything to stdout/console. The output is handled by the code in the editor and it is as follows: Print the elements of linked list from head to tail, each in a new line.
Sample Input
5
383
484
392
975
321
Sample Output
321
975
392
484
383
Explanation
Intially the list in NULL. After inserting 383, the list is 383 -> NULL. After inserting 484, the list is 484 -> 383 -> NULL. After inserting 392, the list is 392 -> 484 -> 383 -> NULL. After inserting 975, the list is 975 -> 392 -> 484 -> 383 -> NULL. After inserting 321, the list is 321 -> 975 -> 392 -> 484 -> 383 -> NULL.
implmentation
code in c++
#include <bits/stdc++.h> using namespace std; class SinglyLinkedListNode { public: int data; SinglyLinkedListNode *next; SinglyLinkedListNode(int node_data) { this->data = node_data; this->next = nullptr; } }; class SinglyLinkedList { public: SinglyLinkedListNode *head; SinglyLinkedListNode *tail; SinglyLinkedList() { this->head = nullptr; this->tail = nullptr; } }; void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) { while (node) { fout << node->data; node = node->next; if (node) { fout << sep; } } } void free_singly_linked_list(SinglyLinkedListNode* node) { while (node) { SinglyLinkedListNode* temp = node; node = node->next; free(temp); } } // Complete the insertNodeAtHead function below. /* * For your reference: * * SinglyLinkedListNode { * int data; * SinglyLinkedListNode* next; * }; * */ SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) { SinglyLinkedListNode * temp = new SinglyLinkedListNode(data); temp->next = NULL; if(llist == NULL) { return temp; } else { temp->next = llist; llist = temp; return temp; } } int main() { ofstream fout(getenv("OUTPUT_PATH")); SinglyLinkedList* llist = new SinglyLinkedList(); int llist_count; cin >> llist_count; cin.ignore(numeric_limits<streamsize>::max(), '\n'); for (int i = 0; i < llist_count; i++) { int llist_item; cin >> llist_item; cin.ignore(numeric_limits<streamsize>::max(), '\n'); SinglyLinkedListNode* llist_head = insertNodeAtHead(llist->head, llist_item); llist->head = llist_head; } print_singly_linked_list(llist->head, "\n", fout); fout << "\n"; free_singly_linked_list(llist->head); fout.close(); return 0; }