合并两个有序非增链表

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct node {
    node *next;
    int data;
};



int main() {
    cout << "l1=";
    int a[50], b[50];
    int m = 1;
    int n = 1;

    while (cin.peek() != '\n') {
        cin >> a[m++];

    }

    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    node* p, * head, * now;
    head = new node;
    head->data = a[1];
    head->next = NULL;
    now = head;
    for (int i = 2; i <= m; i++) {
        p = new node;
        p->data = a[i];
        p->next = NULL;
        now->next = p;
        now = p;

    }

    cout << "l2=";
    while (cin.peek() != '\n') {
        cin >> b[n++];
       
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    node* pp, * headd, * noww;
    headd = new node;
    headd->data = a[1];
    headd->next = NULL;
    noww = headd;
    for (int i = 2; i <= n; i++) {
        pp = new node;
        pp->data = b[i];
        pp->next = NULL;
        noww->next = pp;
        noww = pp;

    }

    node* ppp, * headdd, * nowww;
    headdd = new node;
    headdd->data = 0;
    headdd->next = NULL;
    nowww = headdd;
    for (int i = 2; i < m + n; i++) {
        ppp = new node;
        ppp->data = 0;
        ppp->next = NULL;
        nowww->next = ppp;
        nowww = ppp;
    }


    now = head;
    noww = headd;
    nowww = headdd;

    for (int i = 1; i < n + m; i++) {
        if (now->data >= noww->data) {
            nowww->data = noww->data;
            noww = noww->next;
            nowww = nowww->next;
            if (now->next == NULL) {
                while (noww->next != NULL) {
                    nowww->data = noww->data;
                    nowww = nowww->next;
                    noww = noww->next;
                }
                break;
            }
            if (noww->next == NULL) {
                while (now->next != NULL) {
                    nowww->data = now->data;
                    nowww = nowww->next;
                    now = now->next;
                }
                break;
            }

        }
        else {
            nowww->data = now->data;
            now = now->next;
            nowww = nowww->next;
            if (now->next == NULL) {
                while (noww->next != NULL) {
                    nowww->data = noww->data;
                    nowww = nowww->next;
                    noww = noww->next;
                }
                break;
            }
            if (noww->next == NULL) {
                while (now->next != NULL) {
                    nowww->data = now->data;
                    nowww = nowww->next;
                    now = now->next;
                }
                break;
            }



        }
    }
    nowww = headdd;
    cout << "[";
    while (nowww->next != NULL) {
        cout << nowww->data;
        if (nowww->next->next == NULL) { break; }
        else {cout << ",";}
        
        nowww = nowww->next;
    }
    cout << "]";
    return 0;

}

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值