We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Solution for C.
Had to research "Inserting a node at the end of a linked list" couple of websites but here is my code:
Node*insert(Node*head,intdata){//Create a new nodestructNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;//if head is NULL, it is an empty listif(head==NULL){head=newNode;}//Otherwise, find the last node and add the newNodeelse{structNode*lastNode=head;//last node's next address will be NULL.while(lastNode->next!=NULL){lastNode=lastNode->next;}//add the newNode at the end of the linked listlastNode->next=newNode;}// return head value to print valuesreturnhead;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 15: Linked List
You are viewing a single comment's thread. Return to all comments →
Solution for C. Had to research "Inserting a node at the end of a linked list" couple of websites but here is my code: