I am trying to make a method which receives the first list node of two different linked list's of ints and the method is supposed to insert the second list right before the smallest int of the first list. Here is what i have but it is very confusing and I do not know how to fix it. The other methods of the program are working fine.
public static ListNode insertBeforeSmallest (ListNode headMain, ListNode headInsert) { ListNode start = headMain; ListNode prevNodeMain = headMain; ListNode prevNodeMain2 = headMain; ListNode nodePtrMain = headMain.getNext (); ListNode nodePtrMain2 = headMain.getNext (); ListNode nodePtrInsert = headInsert; int length = Length (prevNodeMain); String lowestObj = ""; for (int j = 0 ; j < length ; j++) { if (j == 0) { lowestObj = ((String)prevNodeMain.getValue ()); prevNodeMain = nodePtrMain; nodePtrMain = nodePtrMain.getNext (); } else if (((String)prevNodeMain.getValue ()).compareTo (nodePtrMain.getValue ()) < 0) { lowestObj = (String)nodePtrMain.getValue (); prevNodeMain = nodePtrMain; nodePtrMain = nodePtrMain.getNext (); } else { prevNodeMain = nodePtrMain; nodePtrMain = nodePtrMain.getNext (); } } while (nodePtrMain2 != null) { if(prevNodeMain2.getNext ().getValue ().equals (lowestObj)) { prevNodeMain2.getNext ().setNext (nodePtrInsert); for (int i = 0 ; i < (Length (nodePtrInsert) - 1) ; i++) { nodePtrInsert.setNext (nodePtrInsert.getNext ()); } nodePtrInsert.setNext (nodePtrMain2); return start; } else { prevNodeMain2 = nodePtrMain2; nodePtrMain2 = nodePtrMain2.getNext (); } } return null; }