-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeTwoSortedLists.cs
77 lines (62 loc) · 1.65 KB
/
MergeTwoSortedLists.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace LeetCodeProblems;
internal static class MergeTwoSortedLists
{
public static void Test()
{
//Input: list1 = [1, 2, 4], list2 = [1, 3, 4]
//Output: [1,1,2,3,4,4]
//Input: list1 = [], list2 = []
//Output: []
//Input: list1 = [], list2 = [0]
//Output: [0]
var list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
var list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
var list3 = MergeTwoLists(list1,list2);
PrintList(list3);
}
public static ListNode MergeTwoLists(ListNode list1, ListNode list2)
{
ListNode dummy = new ListNode();
ListNode current = dummy;
while (list1 != null && list2 != null)
{
if (list1.val <= list2.val)
{
current.next = list1;
list1 = list1.next;
}
else
{
current.next = list2;
list2 = list2.next;
}
current = current.next;
}
current.next = list1 ?? list2;
return dummy.next;
}
public static void PrintList(ListNode head)
{
var current = head;
while (current != null)
{
Console.Write(current.val);
if (current.next != null)
{
Console.Write(" -> ");
}
current = current.next;
}
Console.WriteLine();
}
}
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int val = 0, ListNode next = null)
{
this.val = val;
this.next = next;
}
}