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

前端 未结 24 1370
陌清茗
陌清茗 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 20:55

    Void deleteMiffffdle(Node* head)
    {
         Node* slow_ptr = head;
         Node* fast_ptr = head;
         Node* tmp = head;
         while(slow_ptr->next != NULL && fast_ptr->next != NULL)
         {
            tmp = slow_ptr;
            slow_ptr = slow_ptr->next;
            fast_ptr = fast_ptr->next->next;
         }
         tmp->next = slow_ptr->next;
         free(slow_ptr);
        enter code here
    
    }
    

提交回复
热议问题