27)回文链表
class Solution{
public:
ListNode* reverseList(ListNode* l){
if(!l || !l->next) return l;
ListNode* newl = reverseList(l->next);
l->next->next = l;
l->next = nullptr;
return newl;
}
bool isPalindrome(ListNode* head){
ListNode* fast=head, slow = head;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
if(!fast){
ListNode* temp = slow;
slow = nullptr;
slow = temp;
break;
}
}
ListNode* newhead = reverseList(slow);
while(head && newhead){
if(head->val != newhead->val) return false;
head = head->next;
newhead = newhead->next;
}
return true;
}
};
29)排序的循环链表
class Solution {
public:
Node* insert(Node* head, int insertVal) {
if(head==nullptr){
head = new Node(insertVal);
head->next = head;
return head;
}
auto cur = head;
while(cur->next != head){
if(cur->next->val < cur->val){
if(cur->next->val>=insertVal || cur->val <= insertVal) break;
}
if(cur->next->val>=insertVal && cur->val <= insertVal) break;
cur = cur->next;
}
cur->next = new Node(insertVal, cur->next);
return head;
}
};