Arena

Which answer is better?

Two models, one test, no names. Your pick feeds the ratings; the names appear after you vote.

Skip this pair

Reverse a linked list in C without recursion

Code
PromptWrite a C function that reverses a singly linked list in place, iteratively, and returns the new head.

A good answer: Three pointers, handles the empty list and a single node, no memory allocation.

A
#include <stddef.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *reverse_list(Node *head)
{
    Node *prev = NULL;
    Node *curr = head;
    Node *next;

    while (curr != NULL) {
        next = curr->next;  /* save the rest of the list */
        curr->next = prev;  /* reverse the link */
        prev = curr;        /* advance prev */
        curr = next;        /* advance curr */
    }
    return prev;            /* prev is the new head */
}
Answer A
B
struct node {
    int val;
    struct node *next;
};

struct node *reverse_list(struct node *head)
{
    struct node *prev = NULL;
    while (head) {
        struct node *next = head->next;
        head->next = prev;
        prev = head;
        head = next;
    }
    return prev;
}
Answer B

"Both nailed it" is how tests retire: when nearly every judgement says it, the test stopped measuring anything.

Tell us what broke, or what is missing