Merge two sorted linked lists with unique integers.
Utilisateur anonyme
public void Merge(LinkList list1, LinkList list2) { if (list1.head == null && list2.head == null) { System.out.println("Empty list"); //checks if list is empty } if (list1.head == null) { list2.printList(); } if (list2.head == null) { list1.printList(); } LinkList list3 = new LinkList(); Node a = list1.head; Node b = list2.head; while (a != null && b != null) { if (a.value b.value) { list3.insert(b.value); b = b.next; } else if (a.value == b.value){ //inserts only unique value to the merged list list3.insert(a.value); a = a.next; b = b.next; } } if (a == null) { while (b != null) { list3.insert(b.value); b = b.next; } } if (b == null) { while (a != null) { list3.insert(a.value); a = a.next; } } list3.printList(); }