I need to merge sort two linked lists, each has a header and the elements are all ints.
I've tried adapting some generic code, but have run into a problem - errors that are similar to this one:
split(AlgSet.LLAlgSet) in AlgSet.LLAlgSet cannot be applied to (Node<java.lang.Integer>)
I see how that is happening as I'm giving it an element next instead of an actual list. Can someone help me out? I don't think it'll take much to fix it, and I am not that skilled in Java.
-
public LLAlgSet merge (LLAlgSet list1, LLAlgSet list2) {
-
-
if (list1 == null) return list2;
-
if (list2 == null) return list1;
-
if (list1.element < list2.element) {
-
list1.next = merge (list1.next, list2);
-
return list1;
-
} // end if
-
else {
-
list2.next = merge (list1, list2.next);
-
return list2;
-
} // end else*/
-
-
}
-
-
public LLAlgSet merge_sort (LLAlgSet list1) {
-
if (list1 == null || list1.next == null)
-
return list1; // checks for empty or single list
-
LLAlgSet list2 = split (list1);
-
list1 = merge_sort (list1);
-
list2 = merge_sort (list2);
-
return merge (list1, list2);
-
}
-
-
public LLAlgSet split (LLAlgSet list1) {
-
if (list1 == null || list1.next == null) return null;
-
LLAlgSet list2 = list1.next;
-
list1.next = list2.next;
-
list2.next = split (list2.next);
-
return list2;
-
}