-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreverse-solution.c
56 lines (46 loc) · 1.2 KB
/
reverse-solution.c
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
54
55
56
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int data;
struct _node *next;
} node, *nodeP;
nodeP recursiveReverseList(nodeP first){
if(first == NULL) return NULL;
if(first->next == NULL) return first;
nodeP rest = recursiveReverseList(first->next);
// rest->next = first;
first->next->next = first;
first->next = NULL;
// return first;
return rest;
}
nodeP createNode(int val) {
nodeP newNode = malloc(sizeof(node));
newNode->data = val;
return newNode;
}
void printList(nodeP head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
nodeP head = createNode(1);
head->next = createNode(2);
head->next->next= createNode(3);
head->next->next->next = createNode(4);
printf("Elements in first list: ");
printList(head);
nodeP newHead = recursiveReverseList(head);
printf("Elements in first list after reverse: ");
printList(newHead);
/* Now testing with an empty list */
nodeP secondListHead = NULL;
printf("Elements in second list: ");
printList(secondListHead);
nodeP newSecondListHead = recursiveReverseList(secondListHead);
printf("Elements in second list after reverse: ");
printList(newSecondListHead);
}