Bài toán
In danh sách liên kết theo thứ tự ngược nhưng không thay đổi cấu trúc list (không đảo con trỏ next).
Ví dụ
Input: 29→34→43→56 Output: 56 43 34 29

Phương pháp đệ quy
- Đệ quy đến cuối (head==NULL)
- Khi unwind: in head->data
- List gốc không đổi
Mã nguồn C
#include <stdio.h>
#include <stdlib.h>
struct node { int data; struct node* next; };
void reverse(struct node* head) {
if (!head) return;
reverse(head->next);
printf("%d ", head->data);
}
void push(struct node** head, int data) {
struct node* n = malloc(sizeof(struct node));
n->data = data; n->next = *head; *head = n;
}
int main() {
struct node* head = NULL;
push(&head, 56); push(&head, 43);
push(&head, 34); push(&head, 29);
reverse(head); // List gốc vẫn 29→34→43→56
return 0;
}
Kết quả
56 43 34 29