• 力扣刷题记录11.1-----面试题 02.07. 链表相交



    一、题目

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    二、代码

    /**
     * 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) {
            
    
        ///必定是尾部相交  这个给的比较模糊
    
        ListNode* count_node = new ListNode(0); 
    
        ListNode* A_node = new ListNode(0); 
        ListNode* B_node = new ListNode(0);  
    
        ListNode* return_node = new ListNode(0);  
        return_node=nullptr;
    
        A_node=headA;
        B_node=headB;
    
        int length_of_A=0;
        int length_of_B=0;
    
        count_node=headA;
        while(count_node!=nullptr) 
        {
          length_of_A+=1;
          count_node=count_node->next;
        }
        std::cout<<" length_of_A "<< length_of_A <<std::endl;
    
        count_node=headB;
        while(count_node!=nullptr) 
        {
          length_of_B+=1;
          count_node=count_node->next;
        }
        std::cout<<" length_of_B "<< length_of_B <<std::endl;
    
        int difference_of_length=abs(length_of_B-length_of_A);  //计算长度差值
        std::cout<<" difference_of_length "<< difference_of_length <<std::endl;
        
       //将链表的节点统一
        int count_decrease_difference=0;
        if(length_of_A<length_of_B)
        {
          while(count_decrease_difference<difference_of_length)
           {
            count_decrease_difference=count_decrease_difference+1;
            B_node=B_node->next;
           }
        }
        if(length_of_A>length_of_B)
        {
          while(count_decrease_difference<difference_of_length)
           {
            count_decrease_difference=count_decrease_difference+1;
            A_node=A_node->next;
           }
        }
         
         
         while(A_node!=nullptr)
         {
           std::cout<<" 此时节点A的数值   "<< A_node->val <<std::endl;
           std::cout<<" 此时节点B的数值   "<< B_node->val <<std::endl;
           if(A_node->val==B_node->val)
           {
               return_node=A_node;
               break;
           }
           A_node=A_node->next;
           B_node=B_node->next;
         }
    
         return return_node;              
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    三、运行结果

    判题系统有一些问题,此题运行通过大部分即可。

  • 相关阅读:
    HLA-Face: Joint High-Low Adaptation for Low Light Face Detection 论文阅读笔记
    你听说过OTA吗?
    javascript中的polyfill是什么,polyfill和babel的关系
    MyBatis:The error occurred while setting parameters;foreach语句不生效
    React的thunk中间件
    二叉树神级遍历:Morris遍历
    模拟退火算法
    Java实现RSA加密和验证
    【路径规划】(2) A* 算法求解最短路,附python完整代码
    SpringMVC Day 05 : Spring 中的 Model
  • 原文地址:https://blog.csdn.net/taiyuezyh/article/details/126456197