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* next = head->next;
  12. head->next = nullptr;
  13. while (next != nullptr) {
  14. List* movingHead = next->next;
  15. next->next = head;
  16. head = next;
  17. next = movingHead;
  18. }
  19. return head;
  20. }
  21.  
  22. int main() {
  23. List* head = new List(1);
  24. List* list = head;
  25. for (int index = 2; index < 10; index++) {
  26. List* next = new List(index);
  27. list->next = next;
  28. list = next;
  29. }
  30. head = reverse(head);
  31.  
  32. while (head != nullptr) {
  33. printf("%d\n", head->value);
  34. head = head->next;
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 4300KB
stdin
Standard input is empty
stdout
9
8
7
6
5
4
3
2
1