Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class ListMerger {
- public static LinkedList merge(LinkedList list1, LinkedList list2) {
- LinkedList result = new LinkedList();
- if (list1.count() == list2.count()) {
- Node node1 = list1.head;
- Node node2 = list2.head;
- for (int i = 0; i < list1.count(); i++) {
- result.addInTail(new Node(node1.value + node2.value));
- node1 = node1.next;
- node2 = node2.next;
- }
- }
- return result;
- }
- public static void main(String[] args) {
- LinkedList list1 = new LinkedList();
- list1.addInTail(new Node(1));
- list1.addInTail(new Node(2));
- list1.addInTail(new Node(3));
- LinkedList list2 = new LinkedList();
- list2.addInTail(new Node(1));
- list2.addInTail(new Node(2));
- list2.addInTail(new Node(3));
- LinkedList merged = merge(list1, list2);
- Arrays.stream(merged.toArray()).forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement