-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem21.cs
40 lines (35 loc) · 960 Bytes
/
Problem21.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace Problems;
public sealed class Problem21
{
// https://leetcode.com/problems/merge-two-sorted-lists/description/
public ListNode? MergeTwoLists(ListNode list1, ListNode list2)
{
if (list1 is null && list2 is null) return null;
var vals = new List<int>();
var it = list1;
FillList(vals, it);
it = list2;
FillList(vals, it);
vals.Sort();
var newHead = new ListNode(vals[0]);
it = newHead;
for (var i = 1; i < vals.Count; i++)
{
var node = new ListNode(vals[i]);
it.next = node;
it = it.next;
}
return newHead;
}
private void FillList(List<int> vals, ListNode? iterator)
{
if (iterator is not null)
{
while (iterator is not null)
{
vals.Add(iterator.val);
iterator = iterator.next;
}
}
}
}