The first two lists common node (26)

topic

[Enter the two lists to find their first common node]


1, analysis

  • Method is the use of violence a list (of length m) each element were compared with another list (of length n) of elements, the time complexity is O ( m n ) O(m*n)

  • Further analysis, two lists have a common node, a node from the start, their p-> next identical (i.e., a pointer to the same node). And because the list is one-way linked list, so from the beginning of the first common node, after all their nodes are coincident, until the last end. Example shown below:
    Here Insert Picture Description

  • Then there are two ideas

    • With auxiliary space: Since the common node most likely at the end of the list (there are also extreme case is the first node is a common node, that is, two lists are identical), so you can start from the tail node traversal, and gradually move forward, until find out first a common node. This method needs the stack to the two lists are stored which, as the stack is advanced out, it is first compared tail node. Since this method by means of the two stacks, space complexity is therefore O ( m + n ) O(m+n) , the time complexity is O ( m + n ) O(m+n)
    • Long chain go first method: First, two lists are traversed, to obtain two lengths of chain. Then find the length difference between the two lists is dis, to make a long list dis go first step, and then let go two lists at the same time, and whether it led a relatively common node. Two lists simultaneously to the tail node. This method does not require auxiliary space, while its time complexity is also O ( m + n ) O(m+n) , so that the method is best

2, the code

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        if(pHead1==nullptr || pHead2==nullptr)
            return nullptr;
        ListNode *p1=pHead1, *p2=pHead2;
        int listCount1=0, listCount2=0;
        while(p1!=nullptr)
        {
            ++listCount1;
            p1=p1->next;
        }
        while(p2!=nullptr)
        {
            ++listCount2;
            p2=p2->next;
        }
        p1=pHead1, p2=pHead2;
        unsigned dis=0;
        if(listCount1>listCount2)
        {
            dis=listCount1-listCount2;
            while(dis)
            {
                p1=p1->next;
                dis--;
            }
        }
        else
        {
            dis=listCount2-listCount1;
            while(dis)
            {
                p2=p2->next;
                dis--;
            }
        }
        while(p1!=nullptr && p2!=nullptr)
        {
            if(p1==p2)
            {
                return p1;
            }
            p1=p1->next;
            p2=p2->next;
        }
    }
};
Published 213 original articles · won praise 48 · views 110 000 +

Guess you like

Origin blog.csdn.net/Jeffxu_lib/article/details/104868219