LeetCode.160. 相交链表

2022-11-18 16:22:00 浏览数 (1)

160.

代码语言:javascript复制
int get_list_length(ListNode *head)
{
    int len = 0;
    while(head)
    {
        len   ;
        head = head->next;
    }
    return len;
}
ListNode *forward_long_list(int long_len,int short_len, ListNode *head)
{
    int delta = long_len - short_len;
    while(head && delta)
    {
        head = head->next;
        delta --;
    }
    return head;
}
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int list_A_len = get_list_length(headA);
        int list_B_len = get_list_length(headB);
        if(list_A_len > list_B_len) 
            headA = forward_long_list(list_A_len,list_B_len,headA);
        else
        {
            headB = forward_long_list(list_B_len,list_A_len,headB); 
        }
        while(headA &&headB)
        {
            if(headA == headB)   return headA;
            headA = headA->next;
            headB = headB->next;
        }
        return NULL;

        
    }
};

160.

ode

0 人点赞