{"record":{"id":"11a0cd84b52bbac0","repo":"TheAlgorithms/C-Sharp","slug":"nameof-node-cannot-have-previous-or-next-null-if-it-s-an","errorCode":null,"errorMessage":"{nameof(node)} cannot have Previous or Next null if it's an internal node","messagePattern":"(.+?) cannot have Previous or Next null if it's an internal node","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs","lineNumber":283,"sourceCode":"    /// </summary>\n    /// <param name=\"node\"> Node to be removed.</param>\n    public void RemoveNode(DoublyLinkedListNode<T> node)\n    {\n        if (node == Head)\n        {\n            RemoveHead();\n            return;\n        }\n\n        if (node == Tail)\n        {\n            Remove();\n            return;\n        }\n\n        if (node.Previous is null || node.Next is null)\n        {\n            throw new ArgumentException(\n                $\"{nameof(node)} cannot have Previous or Next null if it's an internal node\");\n        }\n\n        node.Previous.Next = node.Next;\n        node.Next.Previous = node.Previous;\n        Count--;\n    }\n\n    /// <summary>\n    ///     Removes a node that contains the data from the parameter.\n    /// </summary>\n    /// <param name=\"data\"> Data to be removed form the list.</param>\n    public void Remove(T data)\n    {\n        var node = Find(data);\n        RemoveNode(node);\n    }\n","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/LinkedList/DoublyLinkedList/DoublyLinkedList.cs#L265-L301","documentation":"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.","triggerScenarios":"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.","commonSituations":"Holding node references across removal operations, or constructing nodes manually instead of via Add/Insert.","solutions":["Only pass nodes obtained from Add/GetAt that are still in the list; drop references after removal.","Check node.Previous != null && node.Next != null before calling RemoveNode.","Catch ArgumentException and treat it as 'node not currently internal'."],"exampleFix":"// before\nlist.RemoveNode(staleNode); // node already removed\n// after\nif (staleNode.Previous != null && staleNode.Next != null)\n    list.RemoveNode(staleNode);","handlingStrategy":"validation","validationCode":"if (node.Previous != null && node.Next != null)\n    list.RemoveNode(node);","typeGuard":"bool IsLinkedInternal<T>(DoublyLinkedListNode<T> n) => n.Previous != null && n.Next != null;","tryCatchPattern":"try { list.RemoveNode(node); }\ncatch (ArgumentException) { /* node is not an internal, linked node */ }","preventionTips":["Only pass nodes still owned by the list","Discard node references immediately after removal","Never hand-construct nodes for removal"],"tags":["invalid-argument","linked-list","csharp"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}