📜  用于删除双向链表中节点的Java程序

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

用于删除双向链表中节点的Java程序

先决条件:双向链接列表集 1|介绍和插入

编写一个函数来删除双向链表中的给定节点。
原始双向链表

方法:删除双向链表中的一个节点可以分为三大类:

  • 头节点删除后。

  • 删除中间节点后。

  • 删除最后一个节点后。

如果要删除的节点的指针和头指针已知,则所有提到的三种情况都可以分两步处理。

  1. 如果要删除的节点是头节点,则将下一个节点作为头节点。
  2. 如果一个节点被删除,连接被删除节点的下一个和上一个节点。

算法

  • 设要删除的节点为del
  • 如果要删除的节点是头节点,则将头指针更改为下一个当前头。
if headnode == del then
      headnode =  del.nextNode
  • 如果之前的del存在,则将next of previous 设置为del
if del.nextNode != none 
      del.nextNode.previousNode = del.previousNode 
  • 如果存在del的 next ,则将 next 的prev设置为del
if del.previousNode != none 
      del.previousNode.nextNode = del.next
Java
// Java program to delete a node from
// Doubly Linked List
  
// Class for Doubly Linked List
public class DLL 
{
    // Head of list
    Node head; 
  
    // Doubly Linked list Node
    class Node 
    {
        int data;
        Node prev;
        Node next;
  
        // Constructor to create a new 
        // node. next and prev is by 
        // default initialized as null
        Node(int d) { data = d; }
    }
  
    // Adding a node at the front of 
    // the list
    public void push(int new_data)
    {
        // 1. Allocate node
        // 2. Put in the data
        Node new_Node = new Node(new_data);
  
        // 3. Make next of new node as head
        //    and previous as NULL
        new_Node.next = head;
        new_Node.prev = null;
  
        // 4. Change prev of head node to 
        //    new node
        if (head != null)
            head.prev = new_Node;
  
        // 5. Move the head to point to the 
        //    new node
        head = new_Node;
    }
  
    // This function prints contents of 
    // linked list starting from the given node
    public void printlist(Node node)
    {
        Node last = null;
  
        while (node != null) 
        {
            System.out.print(node.data + " ");
            last = node;
            node = node.next;
        }
  
        System.out.println();
    }
  
    // Function to delete a node in a Doubly 
    // Linked List. head_ref --> pointer to 
    // head node pointer. del --> data of node 
    // to be deleted.
    void deleteNode(Node del)
    {
        // Base case
        if (head == null || del == null) 
        {
            return;
        }
  
        // If node to be deleted is head node
        if (head == del) 
        {
            head = del.next;
        }
  
        // Change next only if node to be 
        // deleted is NOT the last node
        if (del.next != null) 
        {
            del.next.prev = del.prev;
        }
  
        // Change prev only if node to be 
        // deleted is NOT the first node
        if (del.prev != null) 
        {
            del.prev.next = del.next;
        }
  
        // Finally, free the memory occupied 
        // by del
        return;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Start with the empty list
        DLL dll = new DLL();
  
        // Insert 2. So linked list becomes 
        // 2->NULL
        dll.push(2);
  
        // Insert 4. So linked list becomes 
        // 4->2->NULL
        dll.push(4);
  
        // Insert 8. So linked list becomes 
        // 8->4->2->NULL
        dll.push(8);
  
        // Insert 10. So linked list becomes 
        // 10->8->4->2->NULL
        dll.push(10);
  
        System.out.print("Created DLL is: ");
        dll.printlist(dll.head);
  
        // Deleting first node
        dll.deleteNode(dll.head);
  
        // List after deleting first node
        // 8->4->2
        System.out.print(
        "List after deleting first node: ");
        dll.printlist(dll.head);
  
        // Deleting middle node from 8->4->2
        dll.deleteNode(dll.head.next);
  
        System.out.print(
        "List after Deleting middle node: ");
        dll.printlist(dll.head);
    }
}


输出:

Original Linked list 10 8 4 2 
Modified Linked list 8

复杂性分析:

  • 时间复杂度: O(1)。
    由于不需要遍历链表,因此时间复杂度是恒定的。
  • 空间复杂度: O(1)。
    由于不需要额外的空间,因此空间复杂度是恒定的。

有关详细信息,请参阅有关删除双向链表中的节点的完整文章!