Intersection of Two Linked Lists

406 篇文章 0 订阅
406 篇文章 0 订阅

1,题目思路

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:
在这里插入图片描述
begin to intersect at node c1.

Example 1:
在这里插入图片描述
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node’s value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:
在这里插入图片描述
Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node’s value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:
在这里插入图片描述
Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

编写程序以找到两个单链表开头的节点。

  • 如果两个链接列表根本没有交集,则返回null。
  • 函数返回后,链接列表必须保留其原始结构。
  • 您可以假设整个链接结构中没有任何循环。
  • 您的代码最好在O(n)时间内运行,并且只使用O(1)内存。

2,题目思路

对于这道题,是非常经典的链表类型的题目,要求获得两个链表交叉的位置(如果不交叉返回为空)。

需要注意的是,两个单链表的交叉,并不是像“染色体”那种形式的交叉,因为一个单链表节点只能有一个后继(next),因此,只要交叉,就相当于二者汇融在了一起。

题目的求解也是老生常谈,先获得两个链表的长度,然后让其中一个长度长的先走(lenA - lenB)的距离,最后,二者指针再一起走,就可以获得相交叉的节点了。

另外,如果我们将这两个链表逆时针旋转90度可以发现,两个相交的链表变成了一个树的形式,如果对于一颗二叉树,其中每个节点都有一个指向父节点的指针,则利用这个办法也可以找到给定两个节点的最近公共父节点

3,代码视线

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

static const auto s = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return nullptr;
}();

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA == nullptr || headB == nullptr)
            return NULL;
        
        int lenA = listLen(headA);
        int lenB = listLen(headB);
        
        if(lenA >= lenB){
            int dis = lenA - lenB;
            while(dis> 0){
                headA = headA->next;
                dis--;
            }
        }
        else{
            int dis = lenB - lenA;
            while(dis > 0){
                headB = headB->next;
                dis--;
            }
        }
        
        while(headA!=nullptr && headB!= nullptr){
            if(headA == headB)
                return headA;
            headA = headA->next;
            headB = headB->next;
        }
        return NULL;
    }
    
    //求链表的长度
    int listLen(ListNode *head){
        if(head == nullptr)
            return 0;
        
        int len = 0;
        while(head!=nullptr){
            len++;
            head = head->next;
        }
        return len;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值