用于在给定约束下删除链表中给定节点的 Javascript 程序

原文:https://www . geesforgeks . org/JavaScript-program-for-delete-给定约束下链表中的给定节点/

给定一个单链表,编写一个函数来删除给定的节点。您的函数必须遵循以下约束: 1)它必须接受指向起始节点的指针作为第一个参数,接受要删除的节点作为第二个参数,即指向头节点的指针不是全局的。 2)它不应该返回指向头节点的指针。 3)它不应该接受指向头节点的指针。 你可以假设链表永远不会变成空的。 让函数名为 deleteNode()。在一个简单的实现中,当要删除的节点是第一个节点时,函数需要修改头指针。正如上一篇文章中所讨论的,当一个函数修改头部指针时,该函数必须使用其中一个给定的方法,这里我们不能使用任何一个方法。 解决方案 我们明确处理待删除节点是第一个节点的情况,我们把下一个节点的数据复制到头上,删除下一个节点。被删除的节点不是头节点的情况可以通过找到前一个节点并改变前一个节点的下一个来正常处理。以下是实现。

java 描述语言

<script>
// javascript program to delete a given node 
// in linked list under given constraints
var head;

     class Node
     {
            constructor(val)
            {
                this.data = val;
                this.next = null;
            }
        }

    function deleteNode( node,  n) 
    {

        // When node to be deleted is head node
        if (node == n)
        {
            if (node.next == null)
            {
                document.write("There is only one node. The list " + "can't be made empty ");
                return;
            }

            /* Copy the data of next node to head */
            node.data = node.next.data;

            // store address of next node
            n = node.next;

            // Remove the link of next node
            node.next = node.next.next;

            // free memory

            return;
        }

        // When not first node, follow the normal deletion process
        // find the previous node
         prev = node;
        while (prev.next != null && prev.next != n) 
        {
            prev = prev.next;
        }

        // Check if node really exists in Linked List
        if (prev.next == null)
        {
            document.write("Given node is not present in Linked List");
            return;
        }

        // Remove node from Linked List
        prev.next = prev.next.next;
        return;
    }

    /* Utility function to print a linked list */
    function printList( head)
    {
        while (head != null)
        {
            document.write(head.data + " ");
            head = head.next;
        }
        document.write("");
    }

        head = new Node(12);
        head.next = new Node(15);
        head.next.next = new Node(10);
        head.next.next.next = new Node(11);
        head.next.next.next.next = new Node(5);
        head.next.next.next.next.next = new Node(6);
        head.next.next.next.next.next.next = new Node(2);
        head.next.next.next.next.next.next.next = new Node(3);

        document.write("Given Linked List :");
        printList(head);
        document.write("");

        // Let us delete the node with value 10
        document.write("<br/>Deleting node :" + head.next.next.data);
        deleteNode(head, head.next.next);

        document.write("<br/>Modified Linked list :");
        printList(head);
        document.write("<br/>");

        // Lets delete the first node
        document.write("Deleting first Node<br/>");
        deleteNode(head, head);
        document.write("Modified Linked List");
        printList(head);

// This code is contributed by todaysgaurav
</script>

输出:

Given Linked List: 12 15 10 11 5 6 2 3

Deleting node 10:
Modified Linked List: 12 15 11 5 6 2 3

Deleting first node
Modified Linked List: 15 11 5 6 2 3

详情请参考在给定约束下删除链表中给定节点的完整文章!