insert a node at a specific position (best solution)-
Node InsertNth(Node head, int data, int position) {
Node newnode=new Node();
newnode.data=data;
//newdata.next=null;
if(position==0)
{
newnode.next=head;
head=newnode;
}
else
{
Node c=new Node();
int count=1;
c=head;
while(count!=position)
{
c=c.next;
count++;
}
newnode.next=c.next;
c.next=newnode;
}
return ;
}
Node InsertNth(Node head, int data, int position) {
Node newnode=new Node();
newnode.data=data;
//newdata.next=null;
if(position==0)
{
newnode.next=head;
head=newnode;
}
else
{
Node c=new Node();
int count=1;
c=head;
while(count!=position)
{
c=c.next;
count++;
}
newnode.next=c.next;
c.next=newnode;
}
return ;
}
This comment has been removed by a blog administrator.
ReplyDelete