/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * @author Senky * @date 2023.04.19 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1 * @brief 计算链表的长度,申请一个和链表长度一样的数组,将链表的数据域存到数组内,遍历数组头尾对比; * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(struct ListNode* head ) { // write code here struct ListNode* p = head; int size = 0; bool result = false; /*计算链表长度*/ while(p) { size++; p = p->next; } /*申请一个等大的数组*/ int* str = malloc(size*sizeof(int)); int i = 0; int j = size - 1; p = head; /*将链表数据域存在数组里*/ while(p) { str[--size] = p->val; p= p->next; } /*判断回文结构*/ while(i <= j) { if(str[i++] == str[j--]) { result = true; } else { result = false; break; } } return result; }