You are viewing a single comment's thread. Return to all comments →
Added belts and braces
Node* InsertNth(Node *head, int data, int position) { Node newNode = (Node)malloc(sizeof(Node)); newNode->data = data;
if (head == NULL) { return newNode; } if (position == 0) { newNode->next = head; return newNode; } Node *currentNode = head; while(currentNode->next != NuLL && position - 1 > 0) { currentNode = currentNode->next; position--; } if(currentNode ->next == NULL) { cout << "No element at position "<< position << endl; return; } newNode->next = currentNode->next; currentNode->next = newNode; return head;
}
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 →
Added belts and braces
Node* InsertNth(Node *head, int data, int position) { Node newNode = (Node)malloc(sizeof(Node)); newNode->data = data;
}