Adding Node at a given position in Linked List

20:16

Adding Node at a given position in Linked List




public void addNodeAtPosition(Node head,int position,int data)
 {
  Node temp=head;
  //create a node
  Node node=new Node(data);
  int c=1;
  //while counter is not equal to the position
  while(c!=position)
  {
   temp=temp.next;
   c++;
  }
  //point node to the next of current node
  node.next=temp.next;
  //point next of current to the node to insert
  temp.next=node;
 }

To Know the basic structure of the linked list click here

You Might Also Like

0 comments