Practice #1
Lesson 10 of 14 in Coddy's Linked List - Data Structures Series #5 course.
Challenge
EasyWrite a function reverseList that gets an integer array arr and returns the reversed array.
Use a linked list to solve this problem! Push each value at the front of the list, then read the values out.
You must use the LinkedList class (provided in linkedlist.<ext> along with node.<ext>) — do not use language built-ins like arrays' built-in reverse, slicing, or stdlib list operations to compute the result.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"
int main() {
char line[8192];
if (!fgets(line, sizeof(line), stdin)) line[0] = '\0';
int arr[4096];
int len = 0;
char* tok = strtok(line, " \t\r\n");
while (tok) { arr[len++] = atoi(tok); tok = strtok(NULL, " \t\r\n"); }
int rs = 0;
int* r = reverseList(arr, len, &rs);
for (int idx = 0; idx < rs; idx++) {
if (idx > 0) printf(" ");
printf("%d", r[idx]);
}
printf("\n");
return 0;
}