|
| 1 | +package by.andd3dfx.collections; |
| 2 | + |
| 3 | +import java.util.Iterator; |
| 4 | +import lombok.AllArgsConstructor; |
| 5 | +import lombok.EqualsAndHashCode; |
| 6 | +import lombok.NoArgsConstructor; |
| 7 | + |
| 8 | +/** |
| 9 | + * <pre> |
| 10 | + * <a href="https://leetcode.com/problems/merge-two-sorted-lists/">Task description</a> |
| 11 | + * |
| 12 | + * You are given the heads of two sorted linked lists list1 and list2. |
| 13 | + * Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. |
| 14 | + * Return the head of the merged linked list. |
| 15 | + * |
| 16 | + * Example 1: |
| 17 | + * <img src="https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg"/> |
| 18 | + * Input: list1 = [1,2,4], list2 = [1,3,4] |
| 19 | + * Output: [1,1,2,3,4,4] |
| 20 | + * |
| 21 | + * Example 2: |
| 22 | + * Input: list1 = [], list2 = [] |
| 23 | + * Output: [] |
| 24 | + * |
| 25 | + * Example 3: |
| 26 | + * Input: list1 = [], list2 = [0] |
| 27 | + * Output: [0] |
| 28 | + * </pre> |
| 29 | + */ |
| 30 | +public class MergeTwoSortedLists { |
| 31 | + |
| 32 | + public static ListNode mergeTwoLists(ListNode list1, ListNode list2) { |
| 33 | + var head = new ListNode(); |
| 34 | + var iterator1 = new MyIterator(list1); |
| 35 | + var iterator2 = new MyIterator(list2); |
| 36 | + |
| 37 | + var curr = head; |
| 38 | + while (iterator1.hasNext() && iterator2.hasNext()) { |
| 39 | + if (iterator1.peek() < iterator2.peek()) { |
| 40 | + curr.next = new ListNode(iterator1.next()); |
| 41 | + } else { |
| 42 | + curr.next = new ListNode(iterator2.next()); |
| 43 | + } |
| 44 | + curr = curr.next; |
| 45 | + } |
| 46 | + while (iterator1.hasNext()) { |
| 47 | + curr.next = new ListNode(iterator1.next()); |
| 48 | + curr = curr.next; |
| 49 | + } |
| 50 | + while (iterator2.hasNext()) { |
| 51 | + curr.next = new ListNode(iterator2.next()); |
| 52 | + curr = curr.next; |
| 53 | + } |
| 54 | + |
| 55 | + return head.next; |
| 56 | + } |
| 57 | + |
| 58 | + public static ListNode mergeTwoLists2(ListNode list1, ListNode list2) { |
| 59 | + if (list1 == null) { |
| 60 | + return list2; |
| 61 | + } |
| 62 | + if (list2 == null) { |
| 63 | + return list1; |
| 64 | + } |
| 65 | + |
| 66 | + if (list1.val < list2.val) { |
| 67 | + list1.next = mergeTwoLists2(list1.next, list2); |
| 68 | + return list1; |
| 69 | + } else { |
| 70 | + list2.next = mergeTwoLists2(list1, list2.next); |
| 71 | + return list2; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public static class MyIterator implements Iterator<Integer> { |
| 76 | + |
| 77 | + private ListNode curr; |
| 78 | + |
| 79 | + public MyIterator(ListNode head) { |
| 80 | + this.curr = head; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean hasNext() { |
| 85 | + return curr != null; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Integer next() { |
| 90 | + Integer res = curr.val; |
| 91 | + curr = curr.next; |
| 92 | + return res; |
| 93 | + } |
| 94 | + |
| 95 | + public Integer peek() { |
| 96 | + return curr.val; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @EqualsAndHashCode |
| 101 | + @NoArgsConstructor |
| 102 | + @AllArgsConstructor |
| 103 | + public static class ListNode { |
| 104 | + |
| 105 | + int val; |
| 106 | + ListNode next; |
| 107 | + |
| 108 | + ListNode(int val) { |
| 109 | + this.val = val; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return "{%d -> %s}".formatted(val, next); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments