Skip to content
TheChatArchiveThe Chat Archive
chatgpt-4o-latest-20250326· OpenAIHugging Face LMArena Human Preference 140K

write a function in c to delete a node from a linked list

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-mini’s answer.
  1. User

    write a function in c to delete a node from a linked list

  2. Assistant

    Sure! Here's a simple C function to delete a node from a singly linked list based on a given key (i.e., the value of the node to delete).


    ✅ Singly Linked List Node Structure

    #include <stdio.h>
    #include <stdlib.h>
    
    // Definition of a node
    struct Node {
        int data;
        struct Node* next;
    };
    

    ✅ Function to Delete a Node by Key

    void deleteNode(struct Node** head_ref, int key) {
        // Store head node
        struct Node* temp = *head_ref;
        struct Node* prev = NULL;
    
        // If head node itself holds the key to be deleted
        if (temp != NULL && temp->data == key) {
            *head_ref = temp->next;  // Changed head
            free(temp);              // Free old head
            return;
        }
    
        // Search for the key to be deleted and keep track of the previous node
        while (temp != NULL && temp->data != key) {
            prev = temp;
            temp = temp->next;
        }
    
        // If key was not present in linked list
        if (temp == NULL) return;
    
        // Unlink the node from linked list
        prev->next = temp->next;
    
        free(temp); // Free memory
    }
    

    ✅ Helper Functions (for Testing)

    // Function to insert a node at the beginning
    void push(struct Node** head_ref, int new_data) {
        struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
        new_node->data = new_data;
        new_node->next = *head_ref;
        *head_ref = new_node;
    }
    
    // Function to print linked list
    void printList(struct Node* node) {
        while (node != NULL) {
            printf("%d -> ", node->data);
            node = node->next;
        }
        printf("NULL\n");
    }
    

    ✅ Example Usage

    int main() {
        struct Node* head = NULL;
    
        // Creating list: 7 -> 1 -> 3 -> 2 -> NULL
        push(&head, 2);
        push(&head, 3);
        push(&head, 1);
        push(&head, 7);
    
        printf("Original Linked List:\n");
        printList(head);
    
        // Delete node with data = 1
        deleteNode(&head, 1);
    
        printf("Linked List after deletion of 1:\n");
        printList(head);
    
        return 0;
    }
    

    💡 Output:

    Original Linked List:
    7 -> 1 -> 3 -> 2 -> NULL
    Linked List after deletion of 1:
    7 -> 3 -> 2 -> NULL
    

    Let me know if you'd like a version that deletes a node by position or works for a doubly linked list!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt