Dado solo un puntero/referencia a un Node que se eliminará en una lista vinculada individualmente, ¿cómo se elimina?

Dado un puntero a un Node a eliminar, elimine el Node. Tenga en cuenta que no tenemos un puntero al Node principal.

Una solución simple es recorrer la lista enlazada hasta que encuentre el Node que desea eliminar. Pero esta solución requiere un puntero al Node principal, lo que contradice el enunciado del problema.
La solución rápida es copiar los datos del siguiente Node al Node a eliminar y eliminar el siguiente Node. Algo como lo siguiente.

C++

#include <bits/stdc++.h>
using namespace std;
  
/* Link list node */
class Node {
public:
    int data;
    Node* next;
};
  
/* Given a reference (pointer to pointer) to the head 
    of a list and an int, push a new node on the front 
    of the list. */
void push(Node** head_ref, int new_data)
{
    /* allocate node */
    Node* new_node = new Node();
  
    /* put in the data */
    new_node->data = new_data;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
void printList(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
}
  
void deleteNode(Node* node)
{
    Node* prev;
    if (node == NULL)
        return;
    else {
        while (node->next != NULL) {
            node->data = node->next->data;
            prev = node;
            node = node->next;
        }
        prev->next = NULL;
    }
}
  
/* Driver code*/
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
  
    /* Use push() to construct below list 
    1->12->1->4->1 */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1);
  
    cout << "Before deleting \n";
    printList(head);
  
    /* I m deleting the head itself. 
        You can check for more cases */
    deleteNode(head);
  
    cout << "\nAfter deleting \n";
    printList(head);
    return 0;
}
  
// This is code is contributed by rathbhupendra

C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
  
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
  
/* Given a reference (pointer to pointer) to the head
    of a list and an int, push a new node on the front
    of the list. */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
  
    /* put in the data  */
    new_node->data = new_data;
  
    /* link the old list off the new node */
    new_node->next = (*head_ref);
  
    /* move the head to point to the new node */
    (*head_ref) = new_node;
}
  
void printList(struct Node* head)
{
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d  ", temp->data);
        temp = temp->next;
    }
}
  
void deleteNode(struct Node* node)
{
    struct Node* prev;
    if (node == NULL)
        return;
    else {
        while (node->next != NULL) {
            node->data = node->next->data;
            prev = node;
            node = node->next;
        }
        prev->next = NULL;
    }
}
  
/* Driver program to test above function*/
int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
  
    /* Use push() to construct below list
    1->12->1->4->1  */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1);
  
    printf("Before deleting \n");
    printList(head);
  
    /* I m deleting the head itself.
        You can check for more cases */
    deleteNode(head);
  
    printf("\nAfter deleting \n");
    printList(head);
    getchar();
    return 0;
}

Java

class LinkedList {
    Node head; // head of the list
  
    class Node {
        int data;
        Node next;
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
  
    /* Given a reference to the head of a list and an int,
        inserts a new Node on the front of the list. */
    public void push(int new_data)
    {
        /* 1. alloc the Node and put the data */
        Node new_Node = new Node(new_data);
  
        /* 2. Make next of new Node as head */
        new_Node.next = head;
  
        /* 3. Move the head to point to new Node */
        head = new_Node;
    }
  
    /* This function prints contents of linked list 
        starting from the given Node */
    public void printList()
    {
        Node tNode = head;
        while (tNode != null) {
            System.out.print(tNode.data + " ");
            tNode = tNode.next;
        }
    }
  
    public void deleteNode(Node Node_ptr)
    {
        Node temp = Node_ptr.next;
        Node_ptr.data = temp.data;
        Node_ptr.next = temp.next;
        temp = null;
    }
  
    public static void main(String[] args)
    {
        LinkedList llist = new LinkedList();
  
        /* Use push() to construct below list
        1->12->1->4->1  */
        llist.push(1);
        llist.push(4);
        llist.push(1);
        llist.push(12);
        llist.push(1);
  
        System.out.println("Before deleting");
        llist.printList();
  
        /* I m deleting the head itself.
        You can check for more cases */
        llist.deleteNode(llist.head);
  
        System.out.println("\nAfter Deleting");
        llist.printList();
    }
}
// This code is contributed by Rajat Mishra

Python

# a class to define a node with 
# data and next pointer
class Node():
  
    # constructor to initialize a new node
    def __init__(self, val = None):
        self.data = val
        self.next = None
  
# push a node to the front of the list
def push(head, val):
  
    # allocate new node
    newnode = Node(val)
  
    # link the first node of the old list to the new node
    newnode.next = head.next
  
    # make the new node as head of the linked list 
    head.next = newnode
  
# function to print the list
def print_list(head):
  
    temp = head.next
    while(temp != None):
        print(temp.data, end = ' ')
        temp = temp.next
    print()
  
# function to delete the node
# the main logic is in this
def delete_node(node):
  
    prev = Node()
  
    if(node == None):
        return
    else:
        while(node.next != None):
            node.data = node.next.data
            prev = node
            node = node.next
  
        prev.next = None
  
  
if __name__ == '__main__':
  
    # allocate an empty header node
    # this is a node that simply points to the
    # first node in the list
    head = Node()
  
    # construct the below linked list
    # 1->12->1->4->1
    push(head, 1)
    push(head, 4)
    push(head, 1)
    push(head, 12)
    push(head, 1)
  
    print('list before deleting:')
    print_list(head)
  
    # deleting the first node in the list
    delete_node(head.next)
  
    print('list after deleting: ')
    print_list(head)
  
# This code is contributed by Adith Bharadwaj

C#

using System;
      
public class LinkedList 
{
    Node head; // head of the list
  
    public class Node
    {
        public int data;
        public Node next;
        public Node(int d)
        {
            data = d;
            next = null;
        }
    }
  
    /* Given a reference to the head of a list and an int,
        inserts a new Node on the front of the list. */
    public void push(int new_data)
    {
        /* 1. alloc the Node and put the data */
        Node new_Node = new Node(new_data);
  
        /* 2. Make next of new Node as head */
        new_Node.next = head;
  
        /* 3. Move the head to point to new Node */
        head = new_Node;
    }
  
    /* This function prints contents of linked list 
        starting from the given Node */
    public void printList()
    {
        Node tNode = head;
        while (tNode != null) 
        {
            Console.Write(tNode.data + " ");
            tNode = tNode.next;
        }
    }
  
    public void deleteNode(Node Node_ptr)
    {
        Node temp = Node_ptr.next;
        Node_ptr.data = temp.data;
        Node_ptr.next = temp.next;
        temp = null;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        LinkedList llist = new LinkedList();
  
        /* Use push() to construct below list
        1->12->1->4->1 */
        llist.push(1);
        llist.push(4);
        llist.push(1);
        llist.push(12);
        llist.push(1);
  
        Console.WriteLine("Before deleting");
        llist.printList();
  
        /* I m deleting the head itself.
        You can check for more cases */
        llist.deleteNode(llist.head);
  
        Console.WriteLine("\nAfter Deleting");
        llist.printList();
    }
}
  
// This code is contributed by 29AjayKumar 

Javascript

<script>
    var head; // head of the list
  
    class Node {
        constructor(val) {
            this.data = val;
            this.next = null;
        }
    }
  
    /*
     * Given a reference to the head of a list and an int, inserts a new Node on the
     * front of the 
     */
     function push(new_data) {
        /* 1. alloc the Node and put the data */
        var new_Node = new Node(new_data);
  
        /* 2. Make next of new Node as head */
        new_Node.next = head;
  
        /* 3. Move the head to point to new Node */
        head = new_Node;
    }
  
    /*
     * This function prints contents of linked list starting from the given Node
     */
     function printList() {
        var tNode = head;
        while (tNode != null) {
            document.write(tNode.data + " ");
            tNode = tNode.next;
        }
    }
  
     function deleteNode(Node_ptr) {
        var temp = Node_ptr.next;
        Node_ptr.data = temp.data;
        Node_ptr.next = temp.next;
        temp = null;
    }
  
      
  
        /*
         * Use push() to construct below list 1->12->1->4->1
         */
        push(1);
        push(4);
        push(1);
        push(12);
        push(1);
  
        document.write("Before deleting<br/>");
        printList();
  
        /*
         * I m deleting the head itself. You can check for more cases
         */
        deleteNode(head);
  
        document.write("<br/>After Deleting <br/>");
       printList();
  
// This code is contributed by todaysgaurav 
</script>

C++

void deleteNode(Node *node)
{
   *node = *(node->next);
}

Java

static void deleteNode(Node node)
{
   node = (node.next);
}
  
// This code is contributed by umadevi9616

Python3

def deleteNode(Node Node):
   Node = (Node.next);
  
# This code is contributed by gauravrajput1 

C#

void deleteNode(Node *node)
{
   *node = *(node->next);
}
  
// This code is contributed by shubhamsingh10

Javascript

function deleteNode(node)
{
   node = (node.next);
}
  
// This code is contributed by gauravrajput1 
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *