-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_list_recursive.cpp
More file actions
53 lines (43 loc) · 886 Bytes
/
array_list_recursive.cpp
File metadata and controls
53 lines (43 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <cstdio>
void recur(int* arr, int len, int index) {
if (index == len) {
return;
}
recur(arr, len, index + 1);
printf("%d\n", arr[index]);
}
struct ListNode {
int val;
ListNode* next;
ListNode(int val) : val(val) {
}
};
void recur_list(ListNode* head) {
if (head == nullptr) {
return;
}
recur_list(head->next);
printf("%d\n", head->val);
}
int main() {
// array
#if 0
int arr[10];
for (int i = 0; i < 10; ++i) {
arr[i] = i + 1;
}
recur(arr,10,0);
#endif
// list
ListNode* node1 = new ListNode(1);
ListNode* node2 = new ListNode(2);
ListNode* node3 = new ListNode(3);
ListNode* node4 = new ListNode(4);
node1->next = node2;
node2->next = node3;
node3->next = node4;
recur_list(node1);
printf("end\n");
return 0;
}