leetcode 160. 相交链表

2023.6.5

         这题要注意的是判断两个节点是否相等不是看val,而是看地址。

首先,代码通过遍历链表A和链表B来计算它们的长度,分别保存在num_Anum_B中。

然后,通过比较链表A和链表B的长度,确定它们之间的长度差,保存在gap中。

接下来,将两个指针cur_Acur_B分别指向链表A和链表B的头节点。

然后,通过移动指针,将较长的链表的指针向前移动gap个位置,使得两个链表的指针处于同一起跑线上。

最后,通过同时移动指针cur_Acur_B,比较它们指向的节点是否相等。如果相等,则找到了相交节点,返回该节点。

如果遍历完链表A和链表B后仍未找到相交节点,则返回空指针。

这段代码的时间复杂度为O(m+n),其中m和n分别是链表A和链表B的长度。

        下面上代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
    {
        int num_A=0;
        int num_B=0;
        int gap=0;
        ListNode* cur_A = headA;
        ListNode* cur_B = headB;

        //计算链表A的长度
        while(cur_A != nullptr)
        {
            num_A++;
            cur_A = cur_A->next;
        }
        //计算链表B的长度
        while(cur_B != nullptr)
        {
            num_B++;
            cur_B = cur_B->next;
        }

        if(num_A > num_B)  gap = num_A - num_B;
        else  gap = num_B - num_A;

        cur_A = headA;
        cur_B = headB;
        //两链表移至同一起跑线
        while(gap)
        {
            if(num_A > num_B)
            {
                cur_A = cur_A->next;
                gap--;
            }
            if(num_A <= num_B)
            {
                cur_B = cur_B->next;
                gap--;
            }
        }
        while(cur_A && cur_B)
        {
            if(cur_A == cur_B)
            {
                return cur_A;
            }
            cur_A = cur_A->next;
            cur_B = cur_B->next;
        }
        return  NULL;
    }
};

2023.9.5

        时隔三个月,二刷。 这次一遍就过了,当时这题还卡了挺久,主要当时不明白示例1中的两链表交叉的节点为什么不是节点值为1的那个节点。 现在想想很简单,因为那两个节点只是碰巧值都为1而已,他们在内存中存储的地址是不一样的。 时隔三个月,代码风格也变了一些:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        //求出AB两链表的长度
        int count_A = 0;
        int count_B = 0;
        ListNode* cur_A = headA;
        ListNode* cur_B = headB;
        while(cur_A)
        {
            count_A++;
            cur_A = cur_A->next;
        }
        while(cur_B)
        {
            count_B++;
            cur_B = cur_B->next;
        }
        //移动两链表至起点相同的位置
        cur_A = headA;
        cur_B = headB;
        if(count_B > count_A)
        {
            int diff = count_B - count_A;
            while(diff--) cur_B = cur_B->next;
        }
        else
        {
            int diff = count_A - count_B;
            while(diff--) cur_A = cur_A->next;
        }
        //两链表共同前进,遇到相同节点则返回
        while(cur_A)
        {
            if(cur_A == cur_B) return cur_A;
            cur_A = cur_A->next;
            cur_B = cur_B->next;
        }
        return nullptr;
    }
};

2023.11.1

        三刷。 java代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == headB) return headA;
        int count_A = 1;
        int count_B = 1;
        ListNode cur_A = headA;
        ListNode cur_B = headB;
        //统计两链表的长度
        while(cur_A.next != null){
            count_A++;
            cur_A = cur_A.next;
        }
        while(cur_B.next != null){
            count_B++;
            cur_B = cur_B.next;
        }
        //同一起跑线
        cur_A = headA;
        cur_B = headB;
        if(count_B > count_A){
            int gap = count_B - count_A;
            while(gap > 0){
                cur_B = cur_B.next;
                gap--;
            }
        }
        if(count_A > count_B){
            int gap = count_A - count_B;
            while(gap > 0){
                cur_A = cur_A.next;
                gap--;
            }
        }
        if(cur_A == cur_B) return cur_A;
        //共同前进
        while(cur_A != null){
            if(cur_A == cur_B){
                return cur_A;
            }
            if(cur_A.next==null || cur_B.next==null) return null;
            cur_A = cur_A.next;
            cur_B = cur_B.next;
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值