Merge two sorted linked lists

  • + 0 comments

    java solution static SinglyLinkedListNode mergeLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) { List numbers = new ArrayList<>(); SinglyLinkedListNode h1 = head1; while(h1 != null){ numbers.add(h1.data); h1=h1.next; } SinglyLinkedListNode h2 = head2; while(h2 != null){ numbers.add(h2.data); h2=h2.next; } Collections.sort(numbers, Comparator.reverseOrder()); SinglyLinkedListNode h3 = new SinglyLinkedListNode(numbers.get(0)); for (int i=1;i