fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct List {
  5. int value = 0;
  6. List* next = nullptr;
  7. List(int x) : value(x) {}
  8. };
  9.  
  10. List* reverse(List* head) {
  11. List* list = head;
  12. List* new_head = list;
  13. if (list->next != nullptr) {
  14. list = reverse(list->next);
  15. new_head = list;
  16. while (list->next != nullptr) {
  17. list = list->next;
  18. }
  19. list->next = head;
  20. head->next = nullptr;
  21. }
  22. return new_head;
  23. }
  24.  
  25. int main() {
  26. List* head = new List(1);
  27. List* list = head;
  28. for (int index = 2; index < 10; index++) {
  29. List* next = new List(index);
  30. list->next = next;
  31. list = next;
  32. }
  33. head = reverse(head);
  34.  
  35. while (head != nullptr) {
  36. printf("%d\n", head->value);
  37. head = head->next;
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 4220KB
stdin
Standard input is empty
stdout
9
8
7
6
5
4
3
2
1