TheAlgorithms/C-Sharp · error · ArgumentException

cannot have Previous or Next null if it's an internal node

Error message

{nameof(node)} cannot have Previous or Next null if it's an internal node

What it means

RemoveNode throws ArgumentException when the given node is neither head nor tail but has a null Previous or Next link, meaning it cannot be unlinked as an internal node. The library expects a node that is genuinely linked between two others.

Solutions

  1. Only pass nodes obtained from Add/GetAt that are still in the list; drop references after removal.
  2. Check node.Previous != null && node.Next != null before calling RemoveNode.
  3. Catch ArgumentException and treat it as 'node not currently internal'.

Example fix

// before
list.RemoveNode(staleNode); // node already removed
// after
if (staleNode.Previous != null && staleNode.Next != null)
    list.RemoveNode(staleNode);
Defensive patterns

Strategy: validation

Validate before calling

if (node.Previous != null && node.Next != null)
    list.RemoveNode(node);

Type guard

bool IsLinkedInternal<T>(DoublyLinkedListNode<T> n) => n.Previous != null && n.Next != null;

Try / catch

try { list.RemoveNode(node); }
catch (ArgumentException) { /* node is not an internal, linked node */ }

Prevention

When it happens

Trigger: Passing a node that was already removed (its links were cleared), a default-constructed/unlinked node, or a head/tail node that slipped past the earlier identity checks.

Common situations: Holding node references across removal operations, or constructing nodes manually instead of via Add/Insert.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/11a0cd84b52bbac0. Report an issue: GitHub.

Appendix: source

Thrown at DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs:283

    /// </summary>
    /// <param name="node"> Node to be removed.</param>
    public void RemoveNode(DoublyLinkedListNode<T> node)
    {
        if (node == Head)
        {
            RemoveHead();
            return;
        }

        if (node == Tail)
        {
            Remove();
            return;
        }

        if (node.Previous is null || node.Next is null)
        {
            throw new ArgumentException(
                $"{nameof(node)} cannot have Previous or Next null if it's an internal node");
        }

        node.Previous.Next = node.Next;
        node.Next.Previous = node.Previous;
        Count--;
    }

    /// <summary>
    ///     Removes a node that contains the data from the parameter.
    /// </summary>
    /// <param name="data"> Data to be removed form the list.</param>
    public void Remove(T data)
    {
        var node = Find(data);
        RemoveNode(node);
    }

View on GitHub (pinned to 96e2905cab)