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

前端 未结 6 1058
天涯浪人
天涯浪人 2020-12-08 05:44

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

6条回答
  •  孤城傲影
    2020-12-08 06:22

    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;
    }
    

提交回复
热议问题