Insert a node at a specific position in a linked list

  • + 2 comments

    C# solution - Help! - Works perfectly in my local IDE but throws weird exceptions in HackerRank

    public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position)
    {
    	var newNode = new SinglyLinkedListNode(data);
    	if (llist == null)
    	{
    		newNode.next = llist;
    		llist = newNode;
    		return newNode;
    	}
    
    	var nodePointer = llist;
    	for (var i = 0; i < position - 1; i++)
    	{
    		nodePointer = nodePointer.next;
    	}
    	newNode.next = nodePointer.next;
    	nodePointer.next = newNode;
    	return llist;
    }
    

    s s

    But in HackerRank, it throws these exceptions:

    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(32,25): warning CS8625: Cannot convert null literal to non-nullable reference type. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(33,25): warning CS8625: Cannot convert null literal to non-nullable reference type. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(31,16): warning CS8618: Non-nullable field 'head' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(31,16): warning CS8618: Non-nullable field 'tail' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(23,25): warning CS8625: Cannot convert null literal to non-nullable reference type. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(21,16): warning CS8618: Non-nullable field 'next' must contain a non-null value when exiting constructor. Consider declaring the field as nullable. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(123,43): error CS0103: The name 'insertNodeAtPosition' does not exist in the current context [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(108,50): warning CS8604: Possible null reference argument for parameter 'path' in 'StreamWriter.StreamWriter(string path, bool append)'. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    /tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.cs(89,28): warning CS8601: Possible null reference assignment. [/tmp/submission/20230626/20/27/hackerrank-51f0534b0b28aeb35ebd9e4b89d2cf73/code/Solution.csproj]
    

    Any suggestions?

    s s