I have a linked list of integers. When I insert a new Node, I need to insert it not at the end, but at the other ... i.e. 2, 4, 5, 8, 11, 12, 33, 55, 58, 102, etc. I think I'm putting it in the right position. See what I'm doing wrong?
Node newNode = new Node(someInt);
Node current = head;
for(int i=0; i<count; i++){
if(current == tail && tail.data < someInt){
tail.next = newNode;
}
if(current.data < someInt && current.next.data >= someInt){
newNode.next = current.next;
current.next = newNode;
}
}
source
share