C语言将两个升序链表合并为一个新的升序链表并返回,我的链表合并代码是哪里出错了?

将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

typedef struct Node{
     int val;
     struct Node *next;
}Node,* ListNode;
 
void printLinkList(ListNode L);
ListNode createTail(ListNode L);

//初始化一个链表
ListNode LinkListInit() {
    Node* L;
    L = (Node*)malloc(sizeof(Node));   //申请结点空间 
    if (L == NULL) { //判断是否有足够的内存空间 
        printf("申请内存空间失败\n");
    }
    L->next = NULL;//将next设置为NULL,初始长度为0的单链表 
    return L;
}
//输入一个升序链表
ListNode createTail(ListNode L) {
    int x;
    Node* s, * r = L;
    printf("输入一个升序链表:\n");
    scanf("%d", &x);
    while (x != 9999) {
        s = (Node*)malloc(sizeof(Node));
        s->val = x;
        r->next = s;
        r = s;
        scanf("%d", &x);
    }
    r->next = NULL;
    return L;
}

//链表拼接
ListNode mergeTwoLists(ListNode L1, ListNode L2) {
    ListNode r, s,P;
    Node* m,*p;
    P = LinkListInit();
    p = P;
    r = L1->next;
    s = L2->next;
    if (r == NULL) {
        return L2;
    }
    else if(s == NULL){
        return L1;
    }
    else if (s == NULL && r == NULL) {
        return 0;
    }
    while (L1 != NULL && L2 != NULL) {
        if (L1->val <= L2->val) {
            m = (Node*)malloc(sizeof(Node));
            m->val = L1->val;
            p->next = m;
            p = m;
            L1 = L1->next;
            p->next = NULL;
        }
        else {
            m = (Node*)malloc(sizeof(Node));
            m->val = L2->val;
            p->next = m;
            p = m;
            L2 = L2->next;
            p->next = NULL;
        }
    }
    if (L1 != NULL) {
        m = (Node*)malloc(sizeof(Node));
        m->val = L1->val;
        p->next = m;
        p = m;
        L1 = L1->next;
        p->next = NULL;
    }
    if (L1 != NULL) {
        m = (Node*)malloc(sizeof(Node));
        m->val = L2->val;
        p->next = m;
        p = m;
        L2 = L2->next;
        p->next = NULL;
    }
    p->next = NULL;
    return P;
}


//输出链表
void printLinkList(ListNode L)
{
    ListNode p;
    p = L->next;
    if (p == NULL) {
        printf(" ");
    }
    while (p)
    {
        //printf("1\n");
        printf("%d ", p->val);
        p = p->next;
    }
}


int main(void) {
    ListNode L1, L2,R,L;
    L1 = LinkListInit();
    L2 = LinkListInit();
    L = LinkListInit();
    R = LinkListInit();
    createTail(L1);
    createTail(L2);
    R = mergeTwoLists(L1, L2);
    printLinkList(R);
    return 0;
}

我的链表合并以及排序的函数(mergeTwoLists)代码是哪里有问题吗,为什么输出的结果这么怪异。。。

效果图片
image.png

阅读 1.5k
1 个回答
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if (l1 == NULL) {
        return l2;
    } else if (l2 == NULL) {
        return l1;
    }

    struct ListNode *dummy = (struct ListNode*) malloc(sizeof(struct ListNode));
    struct ListNode *cur = dummy;

    while (l1 != NULL && l2 != NULL) {
        if (l1->val <= l2->val) {
            cur->next = l1;
            l1 = l1->next;
        } else {
            cur->next = l2;
            l2 = l2->next;
        }
        cur = cur->next;
    }

    if (l1 != NULL) {
        cur->next = l1;
    }
    if (l2 != NULL) {
        cur->next = l2;
    }

    struct ListNode *head = dummy->next;
    free(dummy);
    return head;
}
推荐问题
logo
Microsoft
子站问答
访问
宣传栏