160.链表交点

题目

Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
在这里插入图片描述

分析

  • 由于连个链表的长度不同,没法直接比较,需要移动指针使两个指针分别在两个链表的相同索引处
  • 可以使用长度相减法使指针处于相同索引处,也可以使用补齐法使指针处于相同索引处

代码如下

补齐法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode *p, *q;
    p = headA;
    q = headB;
    //补齐思想,较短的链表先到达末尾,然后跳到较长链表的头节点,这样就相当于对短链表进行了补齐
    while(p != q) { 
        p = (p ? p->next : headB);
        q = (q ? q->next : headA);
    }
    return p;
}

相减法

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    
    struct ListNode *current_node_a = headA;
    struct ListNode *current_node_b = headB;
    int length_a = 1;
    int length_b = 1;
    if (headA == NULL || headB == NULL) return NULL;
    while (current_node_a->next != NULL) {
        current_node_a = current_node_a->next;
        length_a++;
    }

    while (current_node_b->next != NULL) {
        current_node_b = current_node_b->next;
        length_b++;
    }
    current_node_a = headA;
    current_node_b = headB;
    
    if (length_a < length_b) {
        int temp = length_b - length_a;
        while(temp--) current_node_b = current_node_b->next;
    }
    if (length_a > length_b) {
        int temp = length_a - length_b;
        while(temp--) current_node_a = current_node_a->next;
    }
    if (current_node_a == current_node_b) return current_node_a;
    while(current_node_a->next != NULL) {
        if (current_node_a->next == current_node_b->next) return current_node_a->next;
        current_node_a = current_node_a->next;
        current_node_b = current_node_b->next;
    }
    return NULL;

}
### 如何用C语言实现检测两个链表是否相交及其交点 对于不带环的链表,可以通过调整长度差异来解决这个问题。具体来说,先遍历两个链表以获取它们各自的长度,并计算长度差值。较长的那个链表先走几步直到两者剩余部分等长,之后同步前进寻找相同节点。 ```c struct ListNode { int val; struct ListNode *next; }; // 获取链表长度 int getLength(struct ListNode* head){ int length = 0; while(head != NULL){ ++length; head = head->next; } return length; } // 寻找交集起点 struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { if (NULL == headA || NULL == headB) return NULL; int lenA = getLength(headA); int lenB = getLength(headB); // 让curA为最长链表的头,lenA为其长度 struct ListNode *curA = headA; struct ListNode *curB = headB; if(lenB > lenA){ curA = headB; curB = headA; int tmpLen = lenA; lenA = lenB; lenB = tmpLen; } // 长度之差 int gap = lenA - lenB; // 较长链表先行gap步 while(gap--){ curA = curA->next; } // 同时移动双指针直至找到公共结点或到达结尾 while(curA != NULL && curB != NULL){ if(curA == curB) return curA; // 返回相遇的第一个节点作为交点 curA = curA->next; curB = curB->next; } return NULL; // 若无交点则返回null[^1] } ``` 当处理带有环的情况时,则需采用不同策略。如果仅有一个链表有环,则两链表必然不会相交;而若两条链表均有环,则可能存在两种情形——要么完全独立成环互不影响,要么共享同一环的一部分甚至全部重叠形成复杂结构。针对这种情况下的算法设计较为复杂,通常涉及快慢指针技巧用于定位入环位置以及后续路径匹配操作[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值