Practice #1
Lesson 10 of 14 in Coddy's Doubly Linked List - Data Structures Series #6 course.
Challenge
EasyWrite a function isPalindrome that gets an integer array arr (the values of a doubly linked list) and returns true if it reads the same forward and backward, false otherwise.
Use a doubly linked list to solve this problem! Walk one pointer from head forward and another from tail backward; they meet in the middle.
You must use the DoublyLinkedList class (provided in doublylinkedlist.<ext> along with node.<ext>) — do not use language built-ins like arrays' reverse, slicing, sort, 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 r = isPalindrome(arr, len);
printf("%s\n", r ? "true" : "false");
return 0;
}