By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

Merge sort help

P: 1
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.

Expand|Select|Wrap|Line Numbers
  1. public LLAlgSet merge (LLAlgSet list1, LLAlgSet list2) {
  2.  
  3.     if (list1 == null) return list2;
  4.     if (list2 == null) return list1;
  5.     if (list1.element < list2.element) {             
  6.         list1.next = merge (list1.next, list2);
  7.         return list1;
  8.     } // end if
  9.     else {
  10.         list2.next = merge (list1, list2.next);
  11.         return list2;
  12.     } // end else*/
  13.  
  14. }
  15.  
  16.     public LLAlgSet merge_sort (LLAlgSet list1) { 
  17.     if (list1 == null || list1.next == null)
  18.            return list1; // checks for empty or single list
  19.     LLAlgSet list2 = split (list1);
  20.     list1 = merge_sort (list1);
  21.     list2 = merge_sort (list2);
  22.     return merge (list1, list2);
  23.         }
  24.  
  25.     public LLAlgSet split (LLAlgSet list1) {
  26.     if (list1 == null || list1.next == null) return null;
  27.     LLAlgSet list2 = list1.next;
  28.     list1.next = list2.next;
  29.     list2.next = split (list2.next);
  30.     return list2;
  31. }
Mar 4 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.