📌  相关文章
📜  在给定约束下删除链表中给定节点的Java程序

📅  最后修改于: 2022-05-13 01:54:57.767000             🧑  作者: Mango

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

给定一个单链表,编写一个函数来删除给定的节点。您的函数必须遵循以下约束:

  1. 它必须接受指向起始节点的指针作为第一个参数和要删除的节点作为第二个参数,即指向头节点的指针不是全局的。
  2. 它不应该返回指向头节点的指针。
  3. 它不应该接受指向头节点的指针。

您可以假设链接列表永远不会为空。
让函数名称为 deleteNode()。在一个简单的实现中,当要删除的节点是第一个节点时,该函数需要修改头指针。如上一篇文章所述,当一个函数修改头指针时,该函数必须使用给定的方法之一,我们不能在这里使用任何这些方法。
解决方案:
我们明确处理要删除的节点是第一个节点的情况,我们将下一个节点的数据复制到head并删除下一个节点。被删除的节点不是头节点的情况,可以通过查找前一个节点,改变前一个节点的下一个节点来正常处理。以下是实现。

Java
// Java program to delete a given node 
// in linked list under given constraints
class LinkedList 
{
    static Node head;
  
    static class Node 
    {
        int data;
        Node next;
  
        Node(int d) 
        {
            data = d;
            next = null;
        }
    }
  
    void deleteNode(Node node, Node n) 
    {        
        // When node to be deleted is 
        // head node
        if (node == n) 
        {
            if (node.next == null) 
            {
                System.out.println("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
            System.gc();
  
            return;
        }
  
        // When not first node, follow the normal 
        // deletion process find the previous node
        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) {
            System.out.println("Given node is not present in Linked List");
            return;
        }
  
        // Remove node from Linked List
        prev.next = prev.next.next;
  
        // Free memory
        System.gc();
  
        return;
    }
  
    /* Utility function to print a linked list */
    void printList(Node head) {
        while (head != null) {
            System.out.print(head.data + " ");
            head = head.next;
        }
        System.out.println("");
    }
  
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.head = new Node(12);
        list.head.next = new Node(15);
        list.head.next.next = new Node(10);
        list.head.next.next.next = new Node(11);
        list.head.next.next.next.next = new Node(5);
        list.head.next.next.next.next.next = new Node(6);
        list.head.next.next.next.next.next.next = new Node(2);
        list.head.next.next.next.next.next.next.next = new Node(3);
  
        System.out.println("Given Linked List :");
        list.printList(head);
        System.out.println("");
          
        // Let us delete the node with value 10
        System.out.println("Deleting node :" + head.next.next.data);
        list.deleteNode(head, head.next.next);
  
        System.out.println("Modified Linked list :");
        list.printList(head);
        System.out.println("");
  
        // Lets delete the first node
        System.out.println("Deleting first Node");
        list.deleteNode(head, head);
        System.out.println("Modified Linked List");
        list.printList(head);
  
    }
}
  
// this code has been contributed by Mayank Jaiswal


输出:

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

请参阅完整文章在给定约束下删除链表中的给定节点以获取更多详细信息!