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

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;
};



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

运行结果:

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;
}


测试样例能解释一下么,为啥输出是6,7,8,9?

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632