LikeHash 2022-12-01 11:41 采纳率: 100%
浏览 11
已结题

链表基本运用出错,补全代码题

img


题目给的正确模板


#include "stdio.h"
#include <stdlib.h>

struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB);

// 根据数组创建一个链表
struct ListNode *createList(int *arr, int length) {
    struct ListNode *head = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *fakeHead = head;
    for (int i = 0; i < length; ++i) {
        struct ListNode *temp = (struct ListNode *) malloc(sizeof(struct ListNode));
        temp->val = arr[i];
        temp->next = NULL;
        head->next = temp;
        head = head->next;
    }
    return fakeHead->next;
}

// 合并两个链表
struct ListNode *concatList(struct ListNode *head1, struct ListNode *head2) {
    struct ListNode *pre = head1;
    if (head1 == NULL) {
        return head2;
    }
    while (head1->next != NULL) {
        head1 = head1->next;
    }
    head1->next = head2;
    return pre;
}

int main() {
    int n, m, k;
    // 链表A在公共节点前的部分的长度
    scanf("%d", &n);
    // 链表B在公共节点前的部分的长度
    scanf("%d", &m);
    // 公共节点部分长度
    scanf("%d", &k);
    int arrn[n];
    int arrm[m];
    int arrk[k];
    for (int i = 0; i < n; ++i) {
        scanf("%d", &arrn[i]);
    }
    for (int i = 0; i < m; ++i) {
        scanf("%d", &arrm[i]);
    }
    for (int i = 0; i < k; ++i) {
        scanf("%d", &arrk[i]);
    }
    // 创建三个链表   
    struct ListNode *headN = createList(arrn, n);
    struct ListNode *headM = createList(arrm, m);
    struct ListNode *headK = createList(arrk, k);
    // 将公共节点之前的部分和公共部分结合  
    struct ListNode *headA = concatList(headN, headK);
    struct ListNode *headB = concatList(headM, headK);
    // 获取链表的公共节点  
    struct ListNode *res = getIntersectionNode(headA, headB);
    while (res != NULL) {
        printf("%d ", res->val);
        res = res->next;
    }
    return 0;
}

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    // TODO: 填写获取公共节点的代码
}
需填写部分

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
// TODO: 填写获取公共节点的代码
}

出错代码

//思路想运用数组从后往前取公共部分遇到不一样的停止





struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB){
    int n=0;
    int m=0;
    struct ListNode *a=headA;
    struct ListNode *b=headA;
    for(;a->next!=NULL ;n++){
        a=a->next ;
    }
    for(;b->next!=NULL ;m++){
        b=b->next ;
    }
    int A[n];
    int B[m];
    struct ListNode *a1=headA;
    struct ListNode *b1=headB;
    for(int i=0;i<n ;i++){
        A[n-i-1]=a1->next->val ;
        a1=a1->next ;
    }
    for(int i=0;i<m ;i++){
        B[m-i-1]=b1->next->val ;
        b1=b1->next ;
    }
    struct ListNode *tmp;
    int o=0;
    for(int i=0;i<n;i++){
        if(A[i]==B[i])o++;
    }
    for(int i=0;i<o;i++){
        tmp->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        tmp->next->val = A[o-1-i];
        tmp->next->next = NULL;
        tmp = tmp->next;    
        }
        return tmp;
};



出现错误:
一直停到了那儿没输出结束

  • 写回答

3条回答 默认 最新

  • 关注

    运行结果:

    img

    代码:

    
    #include "stdio.h"
    #include <stdlib.h>
    struct ListNode {
        int val;
        struct ListNode* next;
    };
    struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB);
    // 根据数组创建一个链表
    struct ListNode* createList(int* arr, int length) {
        struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* fakeHead = head;
        for (int i = 0; i < length; ++i) {
            struct ListNode* temp = (struct ListNode*)malloc(sizeof(struct ListNode));
            temp->val = arr[i];
            temp->next = NULL;
            head->next = temp;
            head = head->next;
        }
        return fakeHead->next;
    }
    // 合并两个链表
    struct ListNode* concatList(struct ListNode* head1, struct ListNode* head2) {
        struct ListNode* pre = head1;
        if (head1 == NULL) {
            return head2;
        }
        while (head1->next != NULL) {
            head1 = head1->next;
        }
        head1->next = head2;
        return pre;
    }
    int main() {
        int n, m, k;
        // 链表A在公共节点前的部分的长度
        scanf("%d", &n);
        // 链表B在公共节点前的部分的长度
        scanf("%d", &m);
        // 公共节点部分长度
        scanf("%d", &k);
        int arrn[n];
        int arrm[m];
        int arrk[k];
        for (int i = 0; i < n; ++i) {
            scanf("%d", &arrn[i]);
        }
        for (int i = 0; i < m; ++i) {
            scanf("%d", &arrm[i]);
        }
        for (int i = 0; i < k; ++i) {
            scanf("%d", &arrk[i]);
        }
        // 创建三个链表   
        struct ListNode* headN = createList(arrn, n);
        struct ListNode* headM = createList(arrm, m);
        struct ListNode* headK = createList(arrk, k);
        // 将公共节点之前的部分和公共部分结合  
        struct ListNode* headA = concatList(headN, headK);
        struct ListNode* headB = concatList(headM, headK);
        // 获取链表的公共节点  
        struct ListNode* res = getIntersectionNode(headA, headB);
        while (res != NULL) {
            printf("%d ", res->val);
            res = res->next;
        }
        return 0;
    }
    struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
        // TODO: 填写获取公共节点的代码
        struct ListNode* pa, * pb;
        struct ListNode* ph = 0;
        pa = headA;
        pb = headB;
        while (pa)
        {
            while (pb)
            {
                if (pa != pb && pa->next == pb->next)
                {
                    ph = pa->next;
                    return ph;
                }
                else
                    pb = pb->next;
            }
            pa = pa->next;
            pb = headB;
        }
        return ph;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 已采纳回答 12月1日
  • 修改了问题 12月1日
  • 创建了问题 12月1日

悬赏问题

  • ¥15 请问Quartus的Verilog代码怎么写?
  • ¥18 光催化第一性原理计算析氢效率STH怎么计算
  • ¥100 Mac 版foxmail 收邮件问题
  • ¥15 QWebEngineView
  • ¥15 如何使用shufflenet进行手写数字识别
  • ¥15 .net core 同时编辑怎么防止数据串了
  • ¥20 微信小程序播放直播流
  • ¥15 关于迷宫自走单片机循迹小车的知识
  • ¥15 python使用selenium工具爬取网站的问题
  • ¥15 visual studio中c语言用ODBC链接SQL SERVER