21-30
https://leetcode.cn/studyplan/top-100-liked/
class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
int m = matrix.size(), n = matrix[0].size();
int x = 0, y = n-1;
while(x<m && y>=0){
if(matrix[x][y] == target){
return true;
}
if(matrix[x][y] > target){
y--;
}else{
x++;
}
}
return false ;
}
};
//21
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if(headA == nullptr || headB == nullptr) return nullptr;
ListNode *pa = headA, *pb = headB;
while(pa != pb){
pa = pa == nullptr ? headB : pa -> next;
pb = pb == nullptr ? headA : pb -> next;
}
return pa;
}
};
//22
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *prev = nullptr, *curr = head;
while(curr){
ListNode *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
//23
class Solution {
public:
ListNode* middlenode(ListNode* head){
ListNode* slow = head,*fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
ListNode* reverselistnode(ListNode* head){
ListNode *pre =nullptr, *cur = head;
while(cur){
ListNode *nxt = cur->next;
cur->next = pre;
pre = cur;
cur = nxt;
}
return pre;
}
bool isPalindrome(ListNode* head) {
ListNode* mid = middlenode(head);
ListNode* head2 = reverselistnode(mid);
while(head2){
if(head->val != head2->val) return false;
head = head->next;
head2 = head2->next;
}
return true;
}
};
//24
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == nullptr || head->next == nullptr) return false;
ListNode* fast = head->next, * slow = head;
while(fast != slow){
if(fast == nullptr || fast->next == nullptr) return false;
fast = fast->next->next;
slow = slow->next;
}
return true;
}
};
//25
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head, *fast = head;
while(fast){
slow = slow->next;
if(fast->next==nullptr) return nullptr;
fast= fast->next->next;
if(fast == slow){
ListNode* ptr = head;
while(ptr != slow){
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return nullptr;
}
};
//26
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 == nullptr) return l2;
else if(l2 == nullptr) return l1;
else if(l1->val < l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
//27
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
ListNode * head =nullptr, *tail = nullptr;
while(l1 || l2){
int n1 = l1 ? l1->val:0;
int n2 = l2 ? l2->val:0;
int sum = n1+n2+carry;
if(!head){
head = tail = new ListNode(sum%10);
}else{
tail->next = new ListNode(sum%10);
tail = tail->next;
}
carry = sum / 10;
if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
if(carry){
tail->next = new ListNode(carry);
}
return head;
}
};
//28
class Solution {
public:
int getLength(ListNode* head){
int length = 0 ;
while(head){
length++;
head=head->next;
}
return length;
}
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummy = new ListNode(0);
dummy->next = head;
int len = getLength(head);
ListNode *cur = dummy;
for(int i = 0; i<len-n; i++){
cur = cur->next;
}
cur->next = cur->next->next;
return dummy->next;
}
};//29
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == nullptr || head->next == nullptr) return head;
ListNode* nexhead = head->next;
head->next = swapPairs(nexhead->next);
nexhead->next = head;
return nexhead;
}
};