Cattle customer network - a common node wins the first two lists of office-

Title: Enter the two lists to find their first common node.
Method 1: Use the ideas ring. Using two pointers are two from the beginning to traverse the linked list, when traversing a linked list to the end, a jump to another head of the list traversal resumes. Two pointers synchronous traverse, and continuously compared during traversal, when the two pointers, namely a first common node of the two lists.
Note: there will be two pointers equal time, because it is synchronous traversal, the worst case is: When two pointers are traversed again two lists, two pointers are taking away equal, if to this point, like a pointer is a null pointer, they are equal, the output can. If the traversal equal foregoing process, namely a first common node to the output.

class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        if (pHead1==nullptr||pHead2==nullptr) return nullptr;
        ListNode* l1 = pHead1;
        ListNode* l2 = pHead2;
        while(l1!=l2)
        {
            if(l1==nullptr) l1 =pHead2;
            else l1 = l1->next;
            if(l2==nullptr) l2 =pHead1;
            else l2 = l2->next;
        }
        return l1;
    }
};

Solution two:
thinking: compare the length of two lists, so long lists go a few steps, the chain length and short length remaining equal, then sync to traverse. The situation encountered pointer equality, is the answer.

class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        ListNode* l1 = pHead1;
        ListNode* l2 =pHead2;
        int len1= 0,len2=0;
        while(l1)
        {
            l1=l1->next;
            ++len1;
        }
        while(l2)
        {
            l2=l2->next;
            ++len2;
        }
        int diff = abs(len1-len2);
        l1 = pHead1;
        l2 =pHead2;
        while(diff--)
        {
            if(len1>len2) l1=l1->next;
            if(len2>len1) l2=l2->next;
        }
        while(l1!=l2)
        {
            l1=l1->next;
            l2=l2->next;
        }
        return l1;
    }
};

Solution three:
Thinking: two linked list traversal, the nodes are sequentially press-fitted and two different stacks, and then gradually pop up, next a common node that is not equal to the first pointer.

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        stack<ListNode*> s1;
        stack<ListNode*> s2;
        ListNode* l1 =pHead1;
        ListNode* l2 =pHead2;
        while(l1)
        {
            s1.push(l1);
            l1=l1->next;
        }
        while(l2)
        {
            s2.push(l2);
            l2=l2->next;
        }
        while(!s1.empty()&&!s2.empty())
        {
            l1=s1.top();
            l2=s2.top();
            if(l1!=l2)
            {
                return l1->next;
            }
            s1.pop();
            s2.pop();
        }
        if(s1.empty()&&s2.empty())
            return pHead1;
        if(s1.empty())
            return s2.top()->next;
        if(s2.empty())
            return s1.top()->next;
        return nullptr;
    }
};

Guess you like

Origin blog.csdn.net/qq_43387999/article/details/91049960