Skip to content

Commit bba0073

Browse files
committed
Add solution of MergeTwoSortedLists task
1 parent 8a76da4 commit bba0073

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package by.andd3dfx.collections;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import by.andd3dfx.collections.MergeTwoSortedLists.ListNode;
6+
import java.util.function.BiFunction;
7+
import org.junit.Test;
8+
9+
public class MergeTwoSortedListsTest {
10+
11+
@Test
12+
public void testMergeTwoLists() {
13+
testMergeOfTwoList(MergeTwoSortedLists::mergeTwoLists);
14+
}
15+
16+
@Test
17+
public void testMergeTwoLists2() {
18+
testMergeOfTwoList(MergeTwoSortedLists::mergeTwoLists2);
19+
}
20+
21+
public void testMergeOfTwoList(BiFunction<ListNode, ListNode, ListNode> function) {
22+
assertThat(function.apply(null, null))
23+
.isEqualTo(null);
24+
assertThat(function.apply(null, new ListNode(0)))
25+
.isEqualTo(new ListNode(0));
26+
27+
assertThat(function.apply(
28+
new ListNode(1, new ListNode(2, new ListNode(4))),
29+
new ListNode(1, new ListNode(3, new ListNode(4)))
30+
)).isEqualTo(
31+
new ListNode(1,
32+
new ListNode(1,
33+
new ListNode(2,
34+
new ListNode(3,
35+
new ListNode(4,
36+
new ListNode(4))))))
37+
);
38+
39+
assertThat(function.apply(
40+
new ListNode(3, new ListNode(7, new ListNode(90))),
41+
new ListNode(1, new ListNode(4, new ListNode(78, new ListNode(98))))
42+
)).isEqualTo(
43+
new ListNode(1,
44+
new ListNode(3,
45+
new ListNode(4,
46+
new ListNode(7,
47+
new ListNode(78,
48+
new ListNode(90,
49+
new ListNode(98)))))))
50+
);
51+
}
52+
}

0 commit comments

Comments
 (0)