力扣hot100 21-30记录

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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石去皿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值