3217. Delete Nodes From Linked List Present in Array

WHAT TO KNOW - Sep 7 - - Dev Community

<!DOCTYPE html>





Deleting Nodes from a Linked List Present in an Array

<br> body {<br> font-family: Arial, sans-serif;<br> line-height: 1.6;<br> margin: 0;<br> padding: 20px;<br> }</p> <div class="highlight"><pre class="highlight plaintext"><code> h1, h2, h3 { color: #333; } code { font-family: monospace; background-color: #eee; padding: 5px; border-radius: 3px; } pre { background-color: #eee; padding: 10px; border-radius: 5px; overflow-x: auto; } img { max-width: 100%; height: auto; } </code></pre></div> <p>



Deleting Nodes from a Linked List Present in an Array



Introduction



Linked lists are a fundamental data structure in computer science, offering dynamic memory allocation and efficient insertion and deletion operations. This article delves into the intricate process of deleting nodes from a linked list when the list is represented by an array. We will explore the concepts, techniques, and practical examples to demonstrate this important task.



Understanding Linked Lists and Array Representation



A linked list is a sequence of data elements, called nodes, where each node contains a data field and a pointer to the next node in the sequence. The last node typically points to NULL, signifying the end of the list.


Singly Linked List


To represent a linked list using an array, each element in the array represents a node. The index of an element acts as the pointer to the next node. For example, if the element at index 2 points to index 5, it means the node at index 2 is linked to the node at index 5.



The Challenge of Deletion



Deleting nodes from a linked list represented by an array presents a unique challenge. We need to maintain the integrity of the linked list structure while removing the desired node. This involves updating the pointers (array indices) to bypass the deleted node and ensure the list remains connected.



Techniques for Node Deletion


  1. Deletion by Value

This method involves finding the node with the specified value and removing it from the list. The steps are as follows:

  1. Find the node: Traverse the list (array) until you find the node with the target value. Keep track of the previous node (the node before the one to be deleted).
  2. Update the pointers:
    • If the node to be deleted is the first node, set the head of the list to the next node.
    • If the node to be deleted is not the first node, update the "next" pointer of the previous node to point to the node after the deleted node.
  3. Free the deleted node: This step is specific to dynamic memory allocation. In an array representation, this step may not be necessary as the space is already allocated.

Example:

// C++ Code for deleting a node by value
struct Node {
int data;
int next;
};


void deleteNodeByValue(Node* list, int value) {
Node* current = list;
Node* previous = NULL;

while (current != NULL &amp;&amp; current-&gt;data != value) {
    previous = current;
    current = list[current-&gt;next];
}

if (current != NULL) {
    if (previous == NULL) { // First node
        list = list[current-&gt;next];
    } else { // Not the first node
        previous-&gt;next = current-&gt;next;
    }
}

}


  1. Deletion by Index

This method removes the node at a given index in the linked list. The steps are similar to deleting by value, but instead of searching by value, we directly access the node using the index.

  1. Find the previous node: Determine the index of the node preceding the one to be deleted (index - 1).
  2. Update the pointers:
    • If the node to be deleted is the first node, update the head pointer to the next node.
    • If the node to be deleted is not the first node, update the "next" pointer of the previous node to point to the node after the deleted node.
  3. Free the deleted node: Similar to deletion by value, this step may not be necessary in array representation.

Example:

// Python code for deleting a node by index
def delete_node_by_index(list, index):
if index < 0 or index >= len(list):
    return

if index == 0:  # Delete the first node
    list[0] = list[list[0]]
else:  # Delete a node other than the first
    previous_index = list[index - 1]
    list[previous_index] = list[list[previous_index]]

  • Deletion of the Last Node

    Deleting the last node in a linked list represented by an array requires finding the second-to-last node. This node will be the last node after deletion.

    1. Find the second-to-last node: Traverse the list until the "next" pointer of the current node points to the last node.
    2. Update the pointers: Set the "next" pointer of the second-to-last node to NULL.
    3. Free the deleted node: As before, this step might not be necessary in array representation.
  • Example:


    // Java code for deleting the last node
    class Node {
    int data;
    int next;

    public Node(int data, int next) {
    this.data = data;
    this.next = next;
    }
    }

    void deleteLastNode(Node[] list) {
    if (list == null || list.length == 0) {
    return;
    }

    Node current = list[0];
    while (current.next != null) {
        if (list[current.next].next == null) { // Found the second-to-last node
            current.next = null;
            break;
        }
        current = list[current.next];
    }
    

    }






    Conclusion





    Deleting nodes from a linked list represented by an array involves carefully updating pointers to maintain the list's integrity. The techniques of deletion by value, index, and the special case of deleting the last node demonstrate the core principles involved. By understanding these methods, developers can effectively manipulate linked lists in array representations, ensuring efficient data management in various applications.




    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .