Skip to content

Instantly share code, notes, and snippets.

@zivce
Last active April 3, 2021 20:32
Show Gist options
  • Save zivce/cb8c23f5d2cc16f70a47e6143b8b28af to your computer and use it in GitHub Desktop.
Save zivce/cb8c23f5d2cc16f70a47e6143b8b28af to your computer and use it in GitHub Desktop.
public static Node<Integer> InsertIterative(Node<Integer>head,int pos,int element){
if(pos==0)
{
Node<Integer> newNode=new Node<Integer>(element);
newNode.next=head;
head=newNode;
return head;
}
else
{
int l=LengthIterative(head);
if(pos>l)
{
return head;
}
else
{
Node<Integer>prev=head;
int i=1;
while(i<pos)
{
i++;
prev=prev.next;
}
Node<Integer> newNode=new Node<Integer>(element);
Node<Integer>next=prev.next;
prev.next=newNode;
newNode.next=next;
return head;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment