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);
    }
};
相关推荐
老Z先生8 分钟前
矩阵求逆的几种方法
人工智能·物联网·线性代数·算法·机器学习·matlab·矩阵
机械心21 分钟前
自动驾驶规划算法(一):A*算法原理和代码(c++与python)
c++·算法·自动驾驶
乘风破浪的咸鱼君28 分钟前
2024/9/22 leetcode 128题 283题
算法·leetcode
酒鬼猿31 分钟前
选择排序(C语言实现)
数据结构·算法·排序算法
wizard_fire36 分钟前
C++ 排序算法
c++·算法·排序算法
jingling5551 小时前
后端开发刷题 | 合并区间
java·开发语言·数据结构·后端·算法
循环渐进Forward1 小时前
【C++笔试强训】如何成为算法糕手Day5
开发语言·c++·算法·笔试·bfs·牛客
ganjiee00071 小时前
力扣(leetcode)每日一题 2306 公司命名
leetcode
头顶一只喵喵2 小时前
C语言版入门编程题(力扣):数组元素和与数字和的绝对差
c语言·算法·leetcode
yachihaoteng2 小时前
Studying-图论包含的算法总结
算法·图论