建门户网站,app定做开发公司,传媒公司取名字,做企业网站所需要的资料链表回文结构
编写一个函数#xff0c;检查输入的链表是否是回文的。 示例 1#xff1a;
输入#xff1a; 1-2
输出#xff1a; false 示例 2#xff1a;
输入#xff1a; 1-2-2-1
输出#xff1a; true 链表的回文结构#xff0c;应该先找到中间节…链表回文结构
编写一个函数检查输入的链表是否是回文的。 示例 1
输入 1-2
输出 false 示例 2
输入 1-2-2-1
输出 true 链表的回文结构应该先找到中间节点然后进行一次反转。
然后对其值进行比较。 struct ListNode* reverseList(struct ListNode* head){struct ListNode*newHeadNULL;struct ListNode*curhead;while(cur){struct ListNode*nextcur-next;cur-nextnewHead;newHeadcur;curnext; }return newHead;
}
struct ListNode* middleNode(struct ListNode* head) {struct ListNode* fasthead,*slowhead;while(fastfast-next){slowslow-next;fastfast-next-next;}return slow;
}
bool isPalindrome(struct ListNode* head){struct ListNode*midmiddleNode(head);struct ListNode*rHeadreverseList(mid);struct ListNode*curAhead;struct ListNode*curRrHead;while(curAcurR){if(curA-val!curR-val){return false;}else{curAcurA-next;curRcurR-next;}}return true;
}