Deleting any node from a single linked list when only pointer to that node is given

时间秒杀一切 提交于 2020-01-09 09:09:56

问题


This is a question posed to me in an interview.

"A single linked list is there in the memory. You have to delete a node. You need to write a function to delete that node, which takes only the address of the node to be deleted as input and nothing else(including head)"

I gave the answer similar to the one answered in the below post -- Copying the contents of the next node into the node to be deleted and deleting the next one.

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

But the interviewer asked me again, what if I pass the address of the last node. I told him, since the next will be a NULL, copy that NULL into the data field along with the address to the next node which is also NULL. Then he told me there will be a problem of dangling pointers... which I didn't understand a bit. Can some one please throw light into this problem ? Is there a generic solution to this ?

Update (Two days later) : A little bit additional. Considering there is no special node at the end of the list. And the last node points to NULL and if that node is given as input, how to make the before last node point to NULL. Or is it impossible ?

Simply put : If a node is given as input to a function, how to make the pointer that references it, point to NULL


回答1:


Dangling Pointer:

(http://en.wikipedia.org/wiki/Dangling_reference)

Dangling pointers and wild pointers in computer programming are pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations.

Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data.

In your answer, to delete the given node you actually delete the next node, which might be being referenced by a pointer. That's how dangling pointer problem arise.

(1) There are no outside references to the list, as you clarify in a note. (2) Dangling pointer problem can arise, as the interviewer said.

Both (1) and (2) cannot be correct at the same time. Which means there is a misunderstanding somewhere.

About Deleting the Last Node:

But the interviewer asked me again, what if I pass the address of the last node. I told him, since the next will be a NULL, copy that NULL into the data field along with the address to the next node which is also NULL.

I think you are confusing these two things: (1) A pointer p that points to NULL, (2) A linked list node that has NULL in its data field.

Suppose the data structure is a -> b -> c -> d. Writing NULL to d's data field will not magicly make c to have a NULL pointer in its next field.

You can delete the last node if the linked list always has a special last node that will never be deleted. For example, a -> b -> c -> d -> LAST where LAST has a special value in its data field that denotes it is really the last element. Now to delete d, you could delete LAST and write the special value in d's data field.

Maybe these are exactly what you tried to tell during the interview, in which case there must have been some miscommunication between you and the interviewer.




回答2:


Steps:

  1. Copy data from Node(i+1) to Node(i)
  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.

Function:

void delete_node(node* node)
{
    node->Data = node->Next->Data;
    node* temp = node->Next->Next;
    delete(node->Next);
    node->Next = temp;
}



回答3:


then there should be a check in program whether the given node is last node or not.

void delete_node(node* node1)
{
    node* search=head;
    if(node1==head)
    {
        head=head->next;
        search->next=NULL;
        node1->next=NULL;
    }
    while(search->next != node1)
        search=search->next;
    if(node1->next==NULL)
    {
       search->next=NULL;
    }
    else
    {
       search->next=node1->next;
       node1->next=NULL;
    }
    delete node1;
}



回答4:


If there are other elements that are pointing to the next node which will be copied to the current node and then deleted, then this operation will introduce a bug. So in your answer you should have emphasized that your solution only works if there are no outside references to the list.

Your solution works with the last node only if the data structure is augmented with a specific "last node" element. (If you are using Smalltalk, you can write self become: nil No other language has anything similar)

No, there is no generic solution if there are outside references to the list. I think the interviewer wanted to see whether you are really knowledgable in the subject matter or were just repeating a memorized answer.




回答5:


Probably your link list traversing might need to assume that any node that points to null is null node regardless of the value...

a->b->c->d->NULL

so d is null node and the node should not be considered as a node. this way you can save on using special values since they are not correct in a generic sense. other than that you will not have any other way for previous node to point to null.




回答6:


 public void removeNode(Node node){

        /* if no node return null */
        if(node==null) return;

        /* if only 1 node then delete node */
        if(node.getNext()==null) {
            node = null;
            return ;
        }

        /* copy next node data to this node */
        node.data = node.getNext().data();

        /* store the next next node */
        Node second = node.getNext().getNext();

        /* remove next node */
        removeNode(node.getNext());

        /* set the copied node as next */
        node.setNext(second);
     }


来源:https://stackoverflow.com/questions/9362896/deleting-any-node-from-a-single-linked-list-when-only-pointer-to-that-node-is-gi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!