简体   繁体   中英

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.

When copied into LeetCode, I receive the following error when the "Run Code" button is clicked:

Line 18: Char 48: error CS1503: Argument 1: cannot convert from 'ListNode' to 'System.Collections.Generic.LinkedList' (in Driver .cs)

Is LeetCode using an older C# version, which may be causing the error? 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 presents the following template 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. There is no hope that the Leet Code tests will pass when you change the data types.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM