简体   繁体   English

LeetCode MergeTwoSortedLists21 第 18 行:错误 CS1503:参数 1:无法从 'ListNode' 转换为 'System.Collections.Generic.LinkedList<int> '</int>

[英]LeetCode MergeTwoSortedLists21 Line 18: error CS1503: Argument 1: cannot convert from 'ListNode' to 'System.Collections.Generic.LinkedList<int>'

The following code compiles and works in VS 2022. It was constructed under a netcoreapp3.1 console application.以下代码在 VS 2022 中编译和工作。它是在 netcoreapp3.1 控制台应用程序下构建的。

When copied into LeetCode, I receive the following error when the "Run Code" button is clicked:复制到 LeetCode 时,单击“运行代码”按钮时收到以下错误:

Line 18: Char 48: error CS1503: Argument 1: cannot convert from 'ListNode' to 'System.Collections.Generic.LinkedList' (in Driver .cs)第 18 行:字符 48:错误 CS1503:参数 1:无法从“ListNode”转换为“System.Collections.Generic.LinkedList”(在驱动程序.cs 中)

Is LeetCode using an older C# version, which may be causing the error? LeetCode 是否使用了旧的 C# 版本,这可能导致错误? Any suggestions would be greatly appreciated.任何建议将不胜感激。

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);

            
        }
     
     
     
     
     }
}

You tested your code outside of Leet Code, but also changed the signature of the function.您在 Leet Code 之外测试了您的代码,但也更改了 function 的签名。 Leet Code presents the following template 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) {
        
    }
}

You are supposed to work with that.你应该使用它。 But in your code you are using LinkedList , both for the arguments and the return type.但是在您的代码中,您使用的是LinkedList ,用于 arguments 和返回类型。 There is no hope that the Leet Code tests will pass when you change the data types.当您更改数据类型时,Leet Code 测试无法通过。

To get you started, in your offline version, you should have driver code that looks like this:为了让您开始,在您的离线版本中,您应该有如下所示的驱动程序代码:

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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将列表转换为数组会产生“错误 CS1503:参数 1:无法从 &#39;System.Collections.Generic.List 转换<int> &#39; 到 &#39;System.Array&#39;&quot; - Trying to convert list into array yields "error CS1503: Argument 1: cannot convert from 'System.Collections.Generic.List<int>' to 'System.Array' " 出现错误 CS1503:“参数 1:无法从 'System.Diagnostics,PerformanceCounter' 转换为 'int' - Getting Error CS1503: "Argument 1: Cannot convert from 'System.Diagnostics,PerformanceCounter' to 'int' Unity C# 错误:(12,47):错误 CS1503:参数 2:无法从 &#39;System.Collections.Generic.List 转换<UnityEngine.GameObject> &#39; 浮&#39; - Unity C# error: (12,47): error CS1503: Argument 2: cannot convert from 'System.Collections.Generic.List<UnityEngine.GameObject>' to 'float' CS1503 参数 1:无法从 'string' 转换为 'string[*,*]' - CS1503 Argument1: cannot convert from 'string' to 'string[*,*]' C# - CS1503 - 参数 1:无法从“字符串”转换为“整数” - C# - CS1503 - Argument 1: cannot convert from 'string' to 'int' CS1503:参数 1:无法从 'int' 转换为 'byte' C# - CS1503: Argument 1: cannot convert from 'int' to 'byte' C# CS1503:参数 2:无法从 &#39;System.Action[*,*,*] 转换为 System.Action[]&#39; C# 2022 - CS1503: Argument 2: Cannot convert from 'System.Action[*,*,*] to System.Action[]' C# 2022 将文本复制到剪贴板错误CS1503参数2:无法从&#39;string&#39;转换为&#39;System.Windows.Forms.TextDataFormat&#39; - Copying text to clipboard Error CS1503 Argument 2: cannot convert from 'string' to 'System.Windows.Forms.TextDataFormat' 如何修复 CS1503 参数 1:无法从“字符串”转换为“System.Type”? - How to fix CS1503 Argument 1: cannot convert from 'string' to 'System.Type'? CS1503 C# 参数 1:无法从“字符串”转换为“System.Data.SqlClient.SqlCommand” - CS1503 C# Argument 1: cannot convert from 'string' to 'System.Data.SqlClient.SqlCommand'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM