Intersection of Two Linked Lists(链表)

Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A:         a1 → a2

                       c1 → c2 → c3

B:     b1 → b2 → b3
begin to intersect at node c1.

题意:就是求取两个链表的交汇点,要求:无交汇点返回NULL,否则返回交汇点的地址。




暴力思路:O(nm),固定A链表的第一个点,依次遍历B链表看是否有相同的点;接着固定A链表的第二个点,再依次遍历B链表,直到找到相同点为止。hash解:时间复杂度O(n+m),空间O(n)或者O(m)两指针解法:我们发现只要两个链表长度一样,就只需同时后移节点指针比较一个,若其中一个较长呢,其实处理一下,把两个链表变成一样长即可。解法步骤:1.求出两个链表的长度2.若一样长,无需处理;若其中一个较长,则只需让较长的链表先走abs(lengthA-lengthB)步即可。3.同时后移节点指针,直到找到交汇点。代码:
class Solution {
private:
    int getLength(ListNode* head){
        if(head==NULL) return 0;
        ListNode* p=head;
        int res=0;
        while (p->next!=NULL)
        {
            ++res;p=p->next;
        }
        return res;
    }
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        if(headA==NULL||headB==NULL) return NULL;
        int length_A=getLength(headA);
        int length_B=getLength(headB);
        ListNode* pA=headA;
        ListNode* pB=headB;
        int dis=0;
        if (length_A>length_B)
        {
            dis=length_A-length_B;
            while (dis>0)
            {
                --dis;
                pA=pA->next;
            }
        }else if(length_A<length_B)
        {
            dis=length_B-length_A;
            while (dis>0)
            {
                --dis;
                pB=pB->next;
            }
        }
        while (pA!=NULL&&pB!=NULL&&pA!=pB)
        {
            pA=pA->next;
            pB=pB->next;
        }
        if(pA==pB&&pA!=NULL) return pA;
        else return NULL;
    }
};
 
时间: 2024-10-13 06:38:54

Intersection of Two Linked Lists(链表)的相关文章

leetCode 160. Intersection of Two Linked Lists 链表

160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A:          a1 → a2                                          c1 → c2 → c3

[LintCode] Intersection of Two Linked Lists 求两个链表的交点

Write a program to find the node at which the intersection of two singly linked lists begins. Notice If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. Y

leetcode_160题——Intersection of Two Linked Lists(线性表,哈希表)

Intersection of Two Linked Lists Total Accepted: 28581 Total Submissions: 100989My Submissions Question Solution Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists

[LeetCode] 160. Intersection of Two Linked Lists 解题思路

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

【leetcode】Intersection of Two Linked Lists

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes:

160. Intersection of Two Linked Lists(js)

160. Intersection of Two Linked Lists 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

leetcode Intersection of Two Linked Lists python

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. python

LeetCode OJ:Intersection of Two Linked Lists(两个链表的插入)

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 找出第一个合并的节点的位置(或者说插入),注意复杂度为O(N),那么先遍历两个链

160. 两个链表的相交点 Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i