链表操作

[Algo] 链表操作

链表节点类型定义

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

1. 反转链表

// 1. 反转链表
ListNode *reverseList(ListNode *head)
{
    ListNode *pre = nullptr, *next = nullptr;
    while (head != nullptr)
    {
        next = head->next;
        head->next = pre;
        pre = head;
        head = next;
    }
    return pre;
}

2. 合并两个有序链表

// 2. 合并两个有序链表
ListNode *mergeLists(ListNode *head1, ListNode *head2)
{
    if (head1 == nullptr) return head2;
    if (head2 == nullptr) return head1;
    ListNode *newhead = head1->val <= head2->val ? head1 : head2;
    ListNode *tmphead = new ListNode(0), *cur = tmphead;
    while (head1 != nullptr && head2 != nullptr)
    {
        if (head1->val <= head2->val) {cur->next = head1; cur = cur->next; head1 = head1->next;}
        else {cur->next = head2; cur = cur->next; head2 = head2->next;}
    }
    while (head1 != nullptr)
    {
        cur->next = head1;
        cur = cur->next;
        head1 = head1->next;
    }
    while (head2 != nullptr)
    {
        cur->next = head2;
        cur = cur->next;
        head2 = head2->next;
    }
    delete tmphead;
    return newhead;
}

3. 链表求和

// 3. 两个链表求和(模拟十进制求和)
ListNode *addLists(ListNode *head1, ListNode *head2)
{
    if (head1 == nullptr) return head2;
    if (head2 == nullptr) return head1;
    ListNode *tmphead = new ListNode(0), *cur = tmphead;
    int sum, carry = 0;
    while (head1 != nullptr && head2 != nullptr)
    {
        sum = (head1->val + head2->val + carry) % 10;
        carry = (head1->val + head2->val + carry) / 10;
        cur->next = new ListNode(sum);
        cur = cur->next;
        head1 = head1->next;
        head2 = head2->next;
    }
    while (head1 != nullptr)
    {
        sum = (head1->val + carry) % 10;
        carry =  (head1->val + carry) / 10;
        cur->next = new ListNode(sum);
        cur = cur->next;
        head1 = head1->next;
    }
    while (head2 != nullptr)
    {
        sum = (head2->val + carry) % 10;
        carry =  (head2->val + carry) / 10;
        cur->next = new ListNode(sum);
        cur = cur->next;
        head2 = head2->next;
    }
    if (carry == 1) cur->next = new ListNode(1);
    ListNode *newhead = tmphead->next;
    delete tmphead;
    deleteList(head1);
    deleteList(head2);
    return newhead;
}

4. 划分链表

// 4. 划分链表(小于val -> 大于等于val)
ListNode *divideList(ListNode *head, int val)
{
    ListNode *tmp_head = head;
    ListNode *less_head = new ListNode(0), *greater_head = new ListNode(0), *less_tail = less_head, *greater_tail = greater_head;
    while (head != nullptr)
    {
        if (head->val < val) 
        {
            less_tail->next = head;
            less_tail = less_tail->next;
            head = head->next;
        }
        else 
        {
            greater_tail->next = head;
            greater_tail = greater_tail->next;
            head = head->next;
        }
    }
    if (less_tail == less_head || greater_tail == greater_head) {delete less_head; delete greater_head; return tmp_head;}
    less_tail->next = greater_head->next;
    greater_tail->next = nullptr;
    ListNode *newhead = less_head->next;
    delete less_head; 
    delete greater_head;
    return newhead;
}
posted @ 2024-12-03 17:05  yaoguyuan  阅读(5)  评论(0编辑  收藏  举报