Insert a node at a specific position in a linked list

  • + 2 comments

    Java solution, help me to get out from an error.

            SinglyLinkedListNode temp=new SinglyLinkedListNode(data);
            if(llist==null){
                return temp;
            }
            if(position==0){
                temp.next=llist;
                return temp;
            }
            SinglyLinkedListNode nh=llist;
            while(position-1>0){
                nh=nh.next;
                position--;
            }
            temp.next=nh.next;
            nh.next=temp;
            return llist;