Skip to content

Instantly share code, notes, and snippets.

@amulyagaur
Created July 16, 2017 18:34
Show Gist options
  • Save amulyagaur/f5b16f6b38e7f3a6c28fa7586097e68e to your computer and use it in GitHub Desktop.
Save amulyagaur/f5b16f6b38e7f3a6c28fa7586097e68e to your computer and use it in GitHub Desktop.
node* InsertAtPos(node* head,int x,int n)
{
node* temp = new node();
temp->data = x;
temp->next=NULL;
//if one has to insert at the front
if(n==1)
{
temp->next = head;
head=temp;
return head;
}
//iterate to (n-1)th position
node* temp1 = head;
for(int i=1;i<(n-1);i++)
temp1 = temp1->next;
temp->next= temp1->next;
temp1->next=temp;
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment