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.
SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) {
SinglyLinkedListNode* newNode = new SinglyLinkedListNode(data);
if(position == 0){
newNode -> next = llist;
llist = newNode;
return llist;
}else{
SinglyLinkedListNode* current = llist;
int i=0;
while(i<position-1 && current->next!=nullptr){
current = current->next;
i++;
}
if(current->next == nullptr){
newNode->next = nullptr;
current->next = newNode;
}else{
newNode -> next = current->next;
current -> next = newNode;
}
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 →
Sol in C++