题解 | #判断一个链表是否为回文结构#
判断一个链表是否为回文结构
https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f
#include <stdbool.h> struct ListNode* reverse(struct ListNode* head) { if(!head || !head->next) { return head; } struct ListNode *q,*p; p = head; q = head->next; head->next = NULL; while(p) { p = q->next; q->next = head; head = q; q = p; } return head; } bool isPail(struct ListNode* head ) { // write code here struct ListNode* nhead = (struct ListNode*)malloc(sizeof(struct ListNode)); nhead->val = head->val; struct ListNode* tmp = nhead; struct ListNode* tmp1 = head->next; while(tmp1) { struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)); node->val = tmp1->val; tmp->next = node; tmp = tmp->next; tmp1 = tmp1->next; } nhead = reverse(nhead); while(head) { if(head->val != nhead->val) { return false; } head=head->next; nhead=nhead->next; } return true; }