java - Linked List sort -


i saw response https://stackoverflow.com/questions/37538652/adding-an-element-to-a-sorted-list-and-keeping-it-sorted-in-ascending-order-jav thread.

how find position of inserting element?

i'm school student trying linked lists first time can understand @ beginning or end how find right position when have sequence?

public void insertfirstposition(int newelement)     {           listofnversion03 temp = new listofnversion03(thisnumber);         temp.next = next;         next = temp;         thisnumber = newelement;      } 

many thanks.

i expect list have sorted ...

then iterate loop 1 link next. while iterating store previous node. when find first node worst node want insert. change previous node next inserting node , inserting node references current node.

// pseudocode node previousnode = root; while (previousnode.next != null) {     node current = previousnode.next;     // check current node     if (current.value < insert.value) {         previousnode.next = insert;         insert.next = current;         break;     } } 

don't forget handle case insert @ end


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -