You are viewing a single comment's thread. Return to all comments →
here's my solution in C language:
SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) { SinglyLinkedListNode *fnnode = llist; if(llist == NULL || position <= -1){ return llist; } else if (position == 0){ llist = llist->next; free(fnnode); return llist; } else{ int counter = 0; while(fnnode != NULL && counter < position - 1){ fnnode = fnnode -> next; counter++; } if(fnnode != NULL && fnnode->next != NULL){ SinglyLinkedListNode *delf = fnnode -> next; fnnode -> next = fnnode->next->next; free(delf); } } return llist; }
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Delete a Node
You are viewing a single comment's thread. Return to all comments →
here's my solution in C language:
SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) { SinglyLinkedListNode *fnnode = llist; if(llist == NULL || position <= -1){ return llist; } else if (position == 0){ llist = llist->next; free(fnnode); return llist; } else{ int counter = 0; while(fnnode != NULL && counter < position - 1){ fnnode = fnnode -> next; counter++; } if(fnnode != NULL && fnnode->next != NULL){ SinglyLinkedListNode *delf = fnnode -> next; fnnode -> next = fnnode->next->next; free(delf); } } return llist; }