data-structures · intermediate · ~15 min
Three-pointer in-place reversal.
Reverse a singly linked list in place by re-pointing each node's next link, using three pointers.
The grader supplies the same node type:
typedef struct node { int data; struct node *next; } node_t;
Implement node_t *reverse(node_t *head) that reverses the list in place and returns the new head (the old tail). No main — the grader calls it.
head: pointer to the first node, or NULL for an empty list.
The head of the reversed list. For an empty list, return NULL.
reverse(1->2->3) -> 3->2->1
reverse(NULL) -> NULL
reverse(7) -> 7
NULL.head: pointer to the first node, or NULL.
The new head of the reversed list (NULL for an empty list).
Reverse in place; allocate no new nodes.
node_t *reverse(node_t *head) {
/* TODO */
return head;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.