what's wrong with my function to delete node from a circular linked list as some of the testcase are wrong

Viewed 38
Node* deleteNode(Node* head, int key) {
    if(head == NULL){
        return NULL;
    }
    //if their is only one node in list
    if(head -> data == key && head -> next == head){
        delete(head);
        head = NULL;
        return NULL;
    }
    
    // if first node is to be deleted
    Node* last = head;
    Node* temp = head;
    while(last -> next != temp){
        last = last -> next;
    }
    last -> next = temp -> next;
    delete(temp);
    head = last -> next;
    return head;
    

    while(last -> next != head || last -> next -> data != key){
        last = last -> next;
    }
    Node* dum = last -> next;
    last -> next = dum -> next;
    delete(dum);
    
    return head;
}

these are the test cases those are getting wrong

Test Case Input

1 2 3 4 5 -1
3

Your Output

2 3 4 5 -1

Desired Output

1 2 4 5 -1

one more is their that's wrong

Test Case Input

1 2 3 4 5 -1
6

Your Output

2 3 4 5 -1

Desired Output

1 2 3 4 5 -1
0 Answers
Related