Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class ListMerger {
  4. public static LinkedList merge(LinkedList list1, LinkedList list2) {
  5. LinkedList result = new LinkedList();
  6. if (list1.count() == list2.count()) {
  7. Node node1 = list1.head;
  8. Node node2 = list2.head;
  9. for (int i = 0; i < list1.count(); i++) {
  10. result.addInTail(new Node(node1.value + node2.value));
  11. node1 = node1.next;
  12. node2 = node2.next;
  13. }
  14. }
  15.  
  16. return result;
  17. }
  18.  
  19. public static void main(String[] args) {
  20. LinkedList list1 = new LinkedList();
  21. list1.addInTail(new Node(1));
  22. list1.addInTail(new Node(2));
  23. list1.addInTail(new Node(3));
  24.  
  25. LinkedList list2 = new LinkedList();
  26. list2.addInTail(new Node(1));
  27. list2.addInTail(new Node(2));
  28. list2.addInTail(new Node(3));
  29.  
  30. LinkedList merged = merge(list1, list2);
  31.  
  32. Arrays.stream(merged.toArray()).forEach(System.out::println);
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement