public class LinkedList { Node head; public insert(int key) { Node current = head; while(current != null) { current = current.link; } } }Now let's extend the code to stop when we have found the spot to insert the new element. For example, if we are inserting a 15 into the following list, it must go between 11 and 18. But we can't know when current points to the node that contains 11 that we must insert immediately afterward -- there could be a 12 in the next node. But as soon as we get to 18, we know that the new node should be inserted before the node containing 18.
Write a second condition for the while loop that will let the loop continue until current hits the node before which the new node must be inserted.
How should we join the two conditions?
Now we can stop in the following situation. (Again, assume that we are inserting 15.)
To move on to the next step, click here.