Menu
CoddyTech

练习 #1

Coddy 双向链表:数据结构系列 #6 课程的第 10 课,共 14 课。

challenge icon

挑战

简单

编写一个函数 isPalindrome,接收一个整数数组 arr(双向链表的值),如果从前往后和从后往前读取的结果相同,则返回 true,否则返回 false。

使用双向链表解决这个问题!让一个指针从 head 开始向前移动,另一个指针从 tail 开始向后移动,它们会在中间相遇。

你必须使用 DoublyLinkedList 类(该类和 node.<ext> 一起在 doublylinkedlist.<ext> 中提供)。不要使用语言内置功能,例如数组的反转、切片、排序或标准库列表操作来计算结果。

亲手试试

#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;
}

双向链表:数据结构系列 #6中的所有课

自己练习: C 在线编译器