行业知识
力扣细节题:回文链表
2026-11-18 15:16:17
来源:互联网转载
index.php/tags-41973.html" >���意中间节点的寻找
struct ListNode* reverseList(struct ListNode* head){ if(!head) return NULL; struct ListNode*cur=head; struct ListNode*pre=NULL; struct ListNode*next; while(cur){ next=cur->next; cur->next=pre; pre=cur; cur=next; } return pre;}//将链表原地逆置bool isPalindrome(struct ListNode* head){ struct ListNode *p,*q; if(!head||!head->next) return true; //若单链表只有一个或为空则为回文链表 p=head; q=head->next; while(q->next){ p=p->next; q=q->next; if(q->next) q=q->next; } q=p->next; //此时p指向链表中点,q指向链表后半段的第一个结点 q=reverseList(q); p=head; while(q){ if(p->val!=q->val) return false; p=p->next; q=q->next; } return true;}
下一篇:旧服务器怎么防止电子垃圾?