LeetCode MergeTwoSortedLists21 第 18 行:错误 CS1503:参数 1:无法从 'ListNode' 转换为 'System.Collections.Generic.LinkedList<int>'
LeetCode MergeTwoSortedLists21 Line 18: error CS1503: Argument 1: cannot convert from 'ListNode' to 'System.Collections.Generic.LinkedList<int>'
以下代码在 VS 2022 中编译和工作。它是在 netcoreapp3.1 控制台应用程序下构建的。
当复制到 LeetCode 中时,单击“运行 代码”按钮时收到以下错误:
第 18 行:字符 48:错误 CS1503:参数 1:无法从 'ListNode' 转换为 'System.Collections.Generic.LinkedList'(在 Driver.cs 中)
LeetCode 是否使用较旧的 C# 版本,这可能会导致错误?任何建议将不胜感激。
public class Solution {
public LinkedList<int> MergeTwoLists(LinkedList<int> list1, LinkedList<int> list2) {
LinkedList<int> answerList = new LinkedList<int>();
LinkedListNode<int> list1CurrentNode = list1.First; // This is line 18, which is causing the error
LinkedListNode<int> list2CurrentNode = list2.First;
LinkedListNode<int> answerListNode = answerList.First;
// Console.WriteLine(list1.First.Value);
// Console.WriteLine(list2.First.Value);
// Console.WriteLine(list1CurrentNode.Value);
// Console.WriteLine(list2CurrentNode.Value);
if (list1.First == null && list2.First == null)
{
answerList = null;
return answerList;
}
if (((list1.First.Value == 0) || (list2.First.Value == 0)) && ((list1 == null || list2 == null)))
{
answerList.First.Value = 0;
return answerList;
}
if (((list1 == null) || (list2 == null)) && ((list1.First.Value == 0 || list2.First.Value == 0)))
{
answerList.First.Value = 0;
return answerList;
}
while (list1CurrentNode != null && list2CurrentNode != null)
{
if ((list1CurrentNode == list1.First && list2CurrentNode == list2.First) && (list1.First.Value >= list2.First.Value))
{
answerList.AddFirst(list2CurrentNode.Value);
answerListNode = answerList.First;
// Console.WriteLine(answerListNode.Value);
answerList.AddAfter(answerListNode, list1CurrentNode.Value);
// Console.WriteLine(answerList.First.Value);
// Console.WriteLine(answerList.Last.Value);
}
else if ((list1CurrentNode == list1.First && list2CurrentNode == list2.First) && (list1.First.Value < list2.First.Value))
{
answerList.AddFirst(list1CurrentNode.Value);
answerListNode = answerList.First;
// Console.WriteLine(answerListNode.Value);
answerList.AddAfter(answerListNode, list2CurrentNode.Value);
// Console.WriteLine(answerList.First.Value);
// Console.WriteLine(answerList.Last.Value);
}
else if (list1CurrentNode.Value >= list2CurrentNode.Value)
{
answerList.AddLast(list2CurrentNode.Value);
answerList.AddLast(list1CurrentNode.Value);
}
else if (list2CurrentNode.Value > list1CurrentNode.Value)
{
answerList.AddLast(list1CurrentNode.Value);
answerList.AddLast(list2CurrentNode.Value);
}
list1CurrentNode = list1CurrentNode.Next;
list2CurrentNode = list2CurrentNode.Next;
}
// Console.WriteLine(String.Join(" ", answerList));
return answerList;
static void Main(string[] args)
{
LinkedList<int> firstList = new LinkedList<int>();
LinkedList<int> secondList = new LinkedList<int>();
firstList.AddFirst(5);
firstList.AddLast(6);
firstList.AddLast(2);
secondList.AddFirst(7);
secondList.AddLast(1);
secondList.AddLast(9);
Solution solution = new Solution();
solution.MergeTwoLists(firstList, secondList);
}
}
}
您在 Leet Code 之外测试了您的代码,但也更改了函数的签名。 Leet Code 提供了以下模板代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
}
}
你应该使用它。但是在您的代码中,您使用 LinkedList
作为参数和 return 类型。当您更改数据类型时,Leet Code 测试没有希望通过。
为了让你开始,在你的离线版本中,你应该有如下所示的驱动程序代码:
static void Main(string[] args)
{
ListNode firstList = new ListNode(5,
new ListNode(6,
new ListNode(2)));
ListNode secondList = new ListNode(7,
new ListNode(1,
new ListNode(9)));
Solution solution = new Solution();
ListNode mergedList = solution.MergeTwoLists(firstList, secondList);
// Output the returned result
for (ListNode node = mergedList; node != null; node = node.next) {
Console.Write("{0} ", node.val);
}
Console.WriteLine();
}
以下代码在 VS 2022 中编译和工作。它是在 netcoreapp3.1 控制台应用程序下构建的。
当复制到 LeetCode 中时,单击“运行 代码”按钮时收到以下错误:
第 18 行:字符 48:错误 CS1503:参数 1:无法从 'ListNode' 转换为 'System.Collections.Generic.LinkedList'(在 Driver.cs 中)
LeetCode 是否使用较旧的 C# 版本,这可能会导致错误?任何建议将不胜感激。
public class Solution {
public LinkedList<int> MergeTwoLists(LinkedList<int> list1, LinkedList<int> list2) {
LinkedList<int> answerList = new LinkedList<int>();
LinkedListNode<int> list1CurrentNode = list1.First; // This is line 18, which is causing the error
LinkedListNode<int> list2CurrentNode = list2.First;
LinkedListNode<int> answerListNode = answerList.First;
// Console.WriteLine(list1.First.Value);
// Console.WriteLine(list2.First.Value);
// Console.WriteLine(list1CurrentNode.Value);
// Console.WriteLine(list2CurrentNode.Value);
if (list1.First == null && list2.First == null)
{
answerList = null;
return answerList;
}
if (((list1.First.Value == 0) || (list2.First.Value == 0)) && ((list1 == null || list2 == null)))
{
answerList.First.Value = 0;
return answerList;
}
if (((list1 == null) || (list2 == null)) && ((list1.First.Value == 0 || list2.First.Value == 0)))
{
answerList.First.Value = 0;
return answerList;
}
while (list1CurrentNode != null && list2CurrentNode != null)
{
if ((list1CurrentNode == list1.First && list2CurrentNode == list2.First) && (list1.First.Value >= list2.First.Value))
{
answerList.AddFirst(list2CurrentNode.Value);
answerListNode = answerList.First;
// Console.WriteLine(answerListNode.Value);
answerList.AddAfter(answerListNode, list1CurrentNode.Value);
// Console.WriteLine(answerList.First.Value);
// Console.WriteLine(answerList.Last.Value);
}
else if ((list1CurrentNode == list1.First && list2CurrentNode == list2.First) && (list1.First.Value < list2.First.Value))
{
answerList.AddFirst(list1CurrentNode.Value);
answerListNode = answerList.First;
// Console.WriteLine(answerListNode.Value);
answerList.AddAfter(answerListNode, list2CurrentNode.Value);
// Console.WriteLine(answerList.First.Value);
// Console.WriteLine(answerList.Last.Value);
}
else if (list1CurrentNode.Value >= list2CurrentNode.Value)
{
answerList.AddLast(list2CurrentNode.Value);
answerList.AddLast(list1CurrentNode.Value);
}
else if (list2CurrentNode.Value > list1CurrentNode.Value)
{
answerList.AddLast(list1CurrentNode.Value);
answerList.AddLast(list2CurrentNode.Value);
}
list1CurrentNode = list1CurrentNode.Next;
list2CurrentNode = list2CurrentNode.Next;
}
// Console.WriteLine(String.Join(" ", answerList));
return answerList;
static void Main(string[] args)
{
LinkedList<int> firstList = new LinkedList<int>();
LinkedList<int> secondList = new LinkedList<int>();
firstList.AddFirst(5);
firstList.AddLast(6);
firstList.AddLast(2);
secondList.AddFirst(7);
secondList.AddLast(1);
secondList.AddLast(9);
Solution solution = new Solution();
solution.MergeTwoLists(firstList, secondList);
}
}
}
您在 Leet Code 之外测试了您的代码,但也更改了函数的签名。 Leet Code 提供了以下模板代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
}
}
你应该使用它。但是在您的代码中,您使用 LinkedList
作为参数和 return 类型。当您更改数据类型时,Leet Code 测试没有希望通过。
为了让你开始,在你的离线版本中,你应该有如下所示的驱动程序代码:
static void Main(string[] args)
{
ListNode firstList = new ListNode(5,
new ListNode(6,
new ListNode(2)));
ListNode secondList = new ListNode(7,
new ListNode(1,
new ListNode(9)));
Solution solution = new Solution();
ListNode mergedList = solution.MergeTwoLists(firstList, secondList);
// Output the returned result
for (ListNode node = mergedList; node != null; node = node.next) {
Console.Write("{0} ", node.val);
}
Console.WriteLine();
}