🖊作者 : Djx_hmbb
📘专栏 : 数据结构
😆今日分享 : 野马结局 :
答 :非洲有一种吸血蝙蝠,常叮在野马的腿上吸血,不管野马怎样暴怒、狂奔都没用,因此不少野马被活活折磨死。但动物学家发现,蝙蝠所吸的血量极少,远不足以使野马死去,野马的死因在于自身的暴怒和狂奔。于是人们把这种因小事而大动肝火,以致因别人的过失而伤害自己的现象,称之为“野马结局”。
文章目录
✔题目链接
✔题目

✔解题思路 :

- 先找到中间节点,
- 然后把后半部分逆置
- 再将前后两部分一一比对,如果节点的值全部相同,则即为回文。
✔代码详情:
bool chkPalindrome(struct ListNode* A) {
struct ListNode* slow = A;
struct ListNode* fast = A;
//找到中间位置
while (fast != NULL && fast->next != NULL) {
//后移
slow = slow->next;
fast = fast->next->next;
}
struct ListNode* next = slow->next;
struct ListNode* nexttail = next->next;
struct ListNode* prev = A;
//另prev为slow的前一个指针
while(prev->next != slow) {
prev = prev->next;
}
prev->next = NULL;//将slow前面的指针置空
//头插法--倒置slow至fast之间的指针
slow->next = NULL;
next->next = slow;
//后移
slow = next;
next = nexttail;
if (nexttail)
nexttail = nexttail->next;
while (next) {
next->next = slow;
//后移
slow = next;
next = nexttail;
if (nexttail)
nexttail = nexttail->next;
}
//比较
struct ListNode* cur = A;
struct ListNode* curnew = slow;
while (cur && curnew) {
//比较是否一致
if (cur->val != curnew->val) {
return false;
}
else {
//后移
cur = cur->next;
curnew = curnew->next;
}
}
return true;
}
✔总结:
- 这题思维很重要!
- 画图也很重要!
- 思路一定要清晰!!!
感谢家人的阅读,若有不准确的地方 欢迎在评论区指正!
文章介绍了如何判断链表是否具有回文结构,通过寻找链表中间节点并翻转后半部分,然后对比两部分节点值来确定。提供了详细的解题思路和优快云代码实现,强调了清晰思维和画图辅助理解的重要性。

再走呗~
463





