Insert a node at the head of a Linked List - HackerRank Data Structures Solutions in C/C++

preview_player
Показать описание
HackerRank solution for the Data Structures problem solving exercise called "Insert a node at the head of a Linked List". This is video, I am sharing the HackerRank solutions for this coding challenge both in C++ and C programming languages.

Inserting a node at the head of a linked list is as easy as creating a new node, passing it the data to store, setting that new node's next pointer to whatever is the current head of the linked list, and then setting the head pointer in the linked list to the new node.

Here, we are only working with what we call a Singly Linked List. This means that the node in our linked list only have a "next" pointer. They do not have a "back" pointer because we cannot not traverse the list backwards. We can only traverse the linked list forward.

In C++, we can create a new node in our linked list by using the "new" keyword and passing the data integer to our node constructor. In C, however, we're going with the malloc function, which allocates memory for the creation of a new node and returns a pointer. Knowing how to solve linked list problems is important for competitive programming both in C and C++, or any other programming language you might prefer (Java, Python, etc.).

Рекомендации по теме
Комментарии
Автор

Thank you, SIr. very helpful. Sir I request you to suggest me how and where should i start learning Data structures . Because i do not know the basics.

dean