📜  用于查找两个链表的交点的 C 程序(1)

📅  最后修改于: 2023-12-03 15:40:54.222000             🧑  作者: Mango

查找两个链表的交点的 C 程序

在链表中,有时候需要找到两个链表的交点。这个问题可以使用 C 语言很好地解决。下面是一个用于查找两个链表的交点的 C 程序。

程序说明

本程序分别定义两个链表,并模拟产生它们的交点。然后在程序中查找它们的交点。程序中使用了两个指针 p1 和 p2,p1 指向链表1,p2 指向链表2。当 p1 到达链表1 的尽头时,将其重新指向链表2,同样,当 p2 到达链表 2 的尽头时,将其重新指向链表1。当 p1 和 p2 相遇时,它们所指向的结点就是交点。

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

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

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *p1 = headA, *p2 = headB;
    while (p1 != p2) {
        p1 = (p1 == NULL) ? headB : p1->next;
        p2 = (p2 == NULL) ? headA : p2->next;
    }
    return p1;
}

int main() {
    // 构建链表1:1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL
    struct ListNode *a1 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a2 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a3 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a4 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a5 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a6 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a7 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a8 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a9 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a10 = (struct ListNode *) malloc(sizeof(struct ListNode));
    a1->val = 1, a2->val = 2, a3->val = 3, a4->val = 4, a5->val = 5;
    a6->val = 6, a7->val = 7, a8->val = 8, a9->val = 9, a10->val = 10;
    a1->next = a2, a2->next = a3, a3->next = a4, a4->next = a5;
    a5->next = a6, a6->next = a7, a7->next = a8, a8->next = a9;
    a9->next = a10, a10->next = NULL;

    // 构建链表2:15 -> 14 -> 13 -> 12 -> 11 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL
    struct ListNode *b1 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b2 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b3 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b4 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b5 = (struct ListNode *) malloc(sizeof(struct ListNode));
    b1->val = 15, b2->val = 14, b3->val = 13, b4->val = 12, b5->val = 11;
    b1->next = b2, b2->next = b3, b3->next = b4, b4->next = b5;
    b5->next = a6;  // 产生交点
    a6->next = a7, a7->next = a8, a8->next = a9, a9->next = a10;
    a10->next = NULL;

    struct ListNode *intersection = getIntersectionNode(a1, b1);
    if (intersection != NULL) {
        printf("The intersection node is %d\n", intersection->val);
    } else {
        printf("There is no intersection node.\n");
    }
    return 0;
}
Markdown 代码片段
## 程序说明

本程序分别定义两个链表,并模拟产生它们的交点。然后在程序中查找它们的交点。程序中使用了两个指针 p1 和 p2,p1 指向链表1,p2 指向链表2。当 p1 到达链表1 的尽头时,将其重新指向链表2,同样,当 p2 到达链表 2 的尽头时,将其重新指向链表1。当 p1 和 p2 相遇时,它们所指向的结点就是交点。

```c
#include <stdio.h>
#include <stdlib.h>

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

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *p1 = headA, *p2 = headB;
    while (p1 != p2) {
        p1 = (p1 == NULL) ? headB : p1->next;
        p2 = (p2 == NULL) ? headA : p2->next;
    }
    return p1;
}

int main() {
    // 构建链表1:1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL
    struct ListNode *a1 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a2 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a3 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a4 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a5 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a6 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a7 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a8 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a9 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *a10 = (struct ListNode *) malloc(sizeof(struct ListNode));
    a1->val = 1, a2->val = 2, a3->val = 3, a4->val = 4, a5->val = 5;
    a6->val = 6, a7->val = 7, a8->val = 8, a9->val = 9, a10->val = 10;
    a1->next = a2, a2->next = a3, a3->next = a4, a4->next = a5;
    a5->next = a6, a6->next = a7, a7->next = a8, a8->next = a9;
    a9->next = a10, a10->next = NULL;

    // 构建链表2:15 -> 14 -> 13 -> 12 -> 11 -> 6 -> 7 -> 8 -> 9 -> 10 -> NULL
    struct ListNode *b1 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b2 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b3 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b4 = (struct ListNode *) malloc(sizeof(struct ListNode));
    struct ListNode *b5 = (struct ListNode *) malloc(sizeof(struct ListNode));
    b1->val = 15, b2->val = 14, b3->val = 13, b4->val = 12, b5->val = 11;
    b1->next = b2, b2->next = b3, b3->next = b4, b4->next = b5;
    b5->next = a6;  // 产生交点
    a6->next = a7, a7->next = a8, a8->next = a9, a9->next = a10;
    a10->next = NULL;

    struct ListNode *intersection = getIntersectionNode(a1, b1);
    if (intersection != NULL) {
        printf("The intersection node is %d\n", intersection->val);
    } else {
        printf("There is no intersection node.\n");
    }
    return 0;
}

程序输出结果:

The intersection node is 6