LeetCode 刷题 面试题 02.07. 链表相交

思路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) {
        ListNode *dumm1 = new ListNode(0, headA);
        ListNode *dumm2 = new ListNode(0, headB);
        ListNode *curA = dumm1;
        ListNode *curB = dumm2;

        stack<ListNode*> sA;
        stack<ListNode*> sB;        

        while (curA)
        {
            sA.push(curA);
            curA = curA->next;
        }

        while (curB)
        {
            sB.push(curB);
            curB = curB->next;
        }

        if (sA.top() == sB.top())
        {
            ListNode *ans;
            while (sA.top() == sB.top())
            {   
                ans = sA.top();
                sA.pop();
                sB.pop();
                
            }
            return ans;
        }
        else
            return nullptr;
     }
};

方法2 遍历长度

/**
 * 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 *cur1 = headA;
        ListNode *cur2 = headB;
        int len1 = 0, len2 = 0;
        
        while (cur1)
        {
            ++len1;
            cur1 = cur1->next;
        }

        while (cur2)
        {
            ++len2;
            cur2 = cur2->next;
        }

        cur1 = headA;
        cur2 = headB;

        if (len2 > len1)
        {
            swap(len1, len2);
            swap(cur1, cur2);
        }

        int sub = len1 - len2;
        
        while (sub--)
        {
            cur1 = cur1->next;
        }

        while (cur1 != cur2)
        {   
            if (cur1->next == nullptr)
                return nullptr;
            cur1 = cur1->next;
            cur2 = cur2->next;
        }

        return cur1;
    }
};

方法3 官方的解答 用哈希表 更简单一些

/**
 * 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) {
        unordered_set<ListNode *> hashA;

        ListNode *curA = headA;
        ListNode *curB = headB;

        while (curA)
        {
            hashA.insert(curA);
            curA = curA->next;
        }

        while(curB)
        {   
            if (curB == nullptr)
                return nullptr;

            if (hashA.count(curB))
                return curB;

            curB = curB->next;
        }
        return nullptr;
    }
};

累死了 今天就到这了 猪脑过载了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值