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.
My C solution. `
SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) {
SinglyLinkedListNode *newNode = malloc(sizeof(SinglyLinkedListNode *));
newNode->data = data;
if (llist == NULL) {
return newNode;
}
SinglyLinkedListNode *current = llist;
SinglyLinkedListNode *temp = malloc(sizeof(SinglyLinkedListNode *));
//temp->data = data;
for (int i = 0; i < position - 1; i++) {
current = current->next;
}
temp = current->next;
current->next = newNode;
newNode->next = temp;
return llist;
}
`
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insert a node at a specific position in a linked list
You are viewing a single comment's thread. Return to all comments →
My C solution. ` SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { SinglyLinkedListNode *newNode = malloc(sizeof(SinglyLinkedListNode *));
} `