Deleting a middle node from a single linked list when pointer to the previous node is not available

前端 未结 24 1168
陌清茗
陌清茗 2020-11-29 20:42

Is it possible to delete a middle node in the single linked list when the only information available we have is the pointer to the node to be deleted and not the pointer to

相关标签:
24条回答
  • 2020-11-29 21:13

    If you have a linked list A -> B -> C -> D and a pointer to node B. If you have to delete this node you can copy the contents of node C into B, node D into C and delete D. But we cannot delete the node as such in case of a singly linked list since if we do so, node A will also be lost. Though we can backtrack to A in case of doubly linked list.

    Am I right?

    0 讨论(0)
  • 2020-11-29 21:14

    You have the head of the list, right? You just traverse it.

    Let's say that your list is pointed to by "head" and the node to delete it "del".

    C style pseudo-code (dots would be -> in C):

    prev = head
    next = prev.link
    
    while(next != null)
    {
      if(next == del)
      {
        prev.link = next.link;
        free(del);
        del = null;
        return 0;
      }
      prev = next;
      next = next.link;
    }
    
    return 1;
    
    0 讨论(0)
  • 2020-11-29 21:15

    Not possible.

    There are hacks to mimic the deletion.

    But none of then will actually delete the node the pointer is pointing to.

    The popular solution of deleting the following node and copying its contents to the actual node to be deleted has side-effects if you have external pointers pointing to nodes in the list, in which case an external pointer pointing to the following node will become dangling.

    0 讨论(0)
  • 2020-11-29 21:16

    It's definitely more a quiz rather than a real problem. However, if we are allowed to make some assumption, it can be solved in O(1) time. To do it, the strictures the list points to must be copyable. The algorithm is as the following:

    We have a list looking like: ... -> Node(i-1) -> Node(i) -> Node(i+1) -> ... and we need to delete Node(i).

    1. Copy data (not pointer, the data itself) from Node(i+1) to Node(i), the list will look like: ... -> Node(i-1) -> Node(i+1) -> Node(i+1) -> ...
    2. Copy the NEXT of second Node(i+1) into a temporary variable.
    3. Now Delete the second Node(i+1), it doesn't require pointer to the previous node.

    Pseudocode:

    void delete_node(Node* pNode)
    {
        pNode->Data = pNode->Next->Data;  // Assume that SData::operator=(SData&) exists.
        Node* pTemp = pNode->Next->Next;
        delete(pNode->Next);
        pNode->Next = pTemp;
    }
    

    Mike.

    0 讨论(0)
  • 2020-11-29 21:18

    Considering below linked list

    1 -> 2 -> 3 -> NULL

    Pointer to node 2 is given say "ptr".

    We can have pseudo-code which looks something like this:

    struct node* temp = ptr->next;
    ptr->data = temp->data;
    ptr->next = temp->next;
    free(temp);
    
    0 讨论(0)
  • 2020-11-29 21:18
    void delself(list *list)
    {
       /*if we got a pointer to itself how to remove it...*/
       int n;
    
       printf("Enter the num:");
       scanf("%d",&n);
    
       while(list->next!=NULL)
       {
          if(list->number==n) /*now pointer in node itself*/
          {
             list->number=list->next->number;   /*copy all(name,rollnum,mark..)
                                 data of next to current, disconnect its next*/
             list->next=list->next->next;
          }
          list=list->next;
       }
    }
    
    0 讨论(0)
提交回复
热议问题