Insert a node at the head of a linked list hackerrank Solution
Insert a node at the head of a linked listWe have to insert a node at the head of a Linked List. For Example: Input 5 // Number of element of a Linked List 383 484 392 975 321As we have to insert a node at the head of a Linked List. So it will be 383->NULL 484->383->NULL 392->484->383->NULL 975->392->484->383->NULL 321->975->392->484->383->NULL Output 321 975 392 484 383Here, I have represented the logic of the Inserting Node at the head of Linked List in C++. Please Dry and Run the code for the better Understanding. SinglyLinkedListNode* insertNodeAtHead(SinglyLinkedListNode* llist, int data) {
SinglyLinkedListNode* temp =(SinglyLinkedListNode*)(malloc(sizeof(SinglyLinkedListNode))) ;
temp->data =data;
temp->next= NULL;
if(llist==NULL)
{
llist=temp;
}
else{
temp->next = llist;
llist = temp;
}
return llist;
}
Insert node at tail : HackerRank Solution in C++ Cycle Detection: HackerRank Solution in C++ Hacker Rank Solutions: Find Merge Point of Two Lists Hacker Rank Solution: Print the Elements of a Linked List Hacker Rank Solution: Merge two sorted linked lists Sparse Arrays : HackerRank Solution in C++ Left Rotation : HackerRank Solution in C++ Array Manipulation: HackerRank Solution in C++ Forming a Magic Square : HackeRank Solution in C++ Sharing is CaringLike this:Like Loading... Related |