408算法题leetcode--第15天

143. 重排链表

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* findMiddle(ListNode* head){
        ListNode* p = head, *q = head;
        // q移动2步,p移动一步
        while(q->next && q->next->next){
            q = q->next->next;
            p = p->next;
        }
        return p;
    }
    ListNode* reverseList(ListNode* head){
        ListNode* pre = nullptr, *cur = head, *next = nullptr;
        while(cur){
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
    void mergeList(ListNode* l, ListNode* r){
        ListNode* p = l, *q = r;
        while(p && q){
            ListNode* t1 = p->next, *t2 = q->next;
            p->next = q;
            p = t1;
            q->next = p;
            q = t2;
        }
    }
    void reorderList(ListNode* head) {
        if(head == nullptr) return;
        // 思路:找到链表中点,右链表逆转,逐一合并左右链表节点
        // 1. 找中点
        ListNode* mid_head = findMiddle(head);
        ListNode* right_head = mid_head->next;
        mid_head->next = nullptr;
        // 2. 右边翻转
        right_head = reverseList(right_head);
        // 3. 合并两个链表
        mergeList(head, right_head);
    }
};

2. 两数相加

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        // 思路1:读取数字,倒序,相加,倒序,链表
        // 思路2:对应位置相加,进位到后一位
        ListNode* dummy_head = new ListNode();
        ListNode* cur = dummy_head;
        int carry = 0;
        while(l1 || l2 || carry){
            int a = l1 ? l1->val : 0;
            int b = l2 ? l2->val : 0;
            int sum = a + b + carry;
            carry = sum / 10;
            cur->next = new ListNode(sum % 10);
            cur = cur -> next;
            if(l1) l1 = l1->next;
            if(l2) l2 = l2->next;
        }
        return dummy_head->next;
    }
};

445. 两数相加 II

cpp 复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head){
        ListNode* pre = nullptr, *cur = head, *next = nullptr;
        while(cur){
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
    ListNode* addList(ListNode* lhs, ListNode* rhs){
        ListNode* dummy_head = new ListNode();
        ListNode* cur = dummy_head;
        int carry = 0;
        while(carry || lhs || rhs){
            int a = lhs ? lhs->val : 0;
            int b = rhs ? rhs->val : 0;
            int sum = a + b + carry;
            carry = sum / 10;
            cur->next = new ListNode(sum % 10);
            cur = cur->next;
            if(lhs) lhs = lhs->next;
            if(rhs) rhs = rhs->next;
        }
        return dummy_head->next;
    }
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        // 反转2个链表,然后相加
        ListNode* p = reverseList(l1), *q = reverseList(l2);
        ListNode* ret = addList(p, q);
        return reverseList(ret);
    }
};
相关推荐
不会打代码呜呜呜呜3 分钟前
小白零基础--CPP多线程
开发语言·c++·算法
辰尘_星启35 分钟前
【单层神经网络】基于MXNet的线性回归实现(底层实现)
算法·线性回归·mxnet
kcwqxx38 分钟前
day37|完全背包基础+leetcode 518.零钱兑换II ,377.组合总和II
c++·算法·leetcode·动态规划
程序趣谈1 小时前
算法随笔_36: 复写零
数据结构·python·算法
九亿AI算法优化工作室&1 小时前
GWO优化LSBooST回归预测matlab
人工智能·python·算法·机器学习·matlab·数据挖掘·回归
sjsjs113 小时前
【数据结构-字典树】力扣14. 最长公共前缀
数据结构·leetcode
python算法(魔法师版)3 小时前
基于机器学习鉴别中药材的方法
深度学习·线性代数·算法·机器学习·支持向量机·数据挖掘·动态规划
JNU freshman4 小时前
力扣第435场周赛讲解
算法·leetcode·蓝桥杯
眼镜哥(with glasses)4 小时前
蓝桥杯python基础算法(2-2)——基础算法(B)——模拟(上)
算法
赵鑫亿5 小时前
7.DP算法
算法·dp