Niuke Top101 JS implementation to get the first common node of two linked lists

describe

Input two acyclic one-way linked lists, find their first common node, and return empty if there is no common node. (Note that because the incoming data is a linked list, the error test data prompt is displayed in other ways to ensure that the incoming data is correct)

Data range: n≤1000
Requirements: space complexity O(1), time complexity O(n)

For example, when entering {1,2,3},{4,5},{6,7}, the structure of two acyclic one-way linked lists is shown in the following figure:

 

It can be seen that the node value of their first common node is 6, so the node with the node value of 6 is returned.

Enter a description:

The input is divided into 3 sections, the first section is the non-public part of the first linked list, the second section is the non-public part of the second linked list, and the third section is the common part of the first linked list and the second linked list. The background will assemble these 3 parameters into two linked lists, and pass the head nodes corresponding to these two linked lists into the function FindFirstCommonNode, and the input that the user gets is only pHead1 and pHead2.

Return value description:

Return the first common node of pHead1 and pHead2 passed in, and the linked list with this node as the head node will be printed in the background.

Example 1

enter:

{1,2,3},{4,5},{6,7}

return value:

{6,7}

illustrate:

The first parameter {1,2,3} represents the non-public part of the first linked list, the second parameter {4,5} represents the non-public part of the second linked list, and the last {6,7} represents the The public part of the 2 linked lists 
These 3 parameters will be assembled in the background into 2 two acyclic single linked lists with common nodes          

Example 2

enter:

{1},{2,3},{}

return value:

{}

illustrate:

2 linked lists have no common nodes, return null, and print {} in the background      

Idea: first judge whether linked list 1 is empty or whether linked list 2 is empty or both linked lists are empty, then return null, and then get the length of the two linked lists through traversal, if the length of linked list 1 is greater than or equal to the length of linked list 2, give the linked list 1 Each position has a flag value, and then traverses the linked list 2. If the flag value is traversed, it returns to the following linked list. The idea that the length of the linked list 2 is greater than the length of the linked list 1 is the same, so I won’t repeat it here.

The complete code is as follows: 

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function FindFirstCommonNode(pHead1, pHead2)
{
    // write code here
    // 先遍历pHead1
    let p1=pHead1;
    let p2=pHead2;
    let n1,n2=0;
    if(p1===null||p2===null||p1===null&&p2===null) {
        return null;
    }
    while(p1) {
        n1++;
        p1=p1.next;
    }
    while(p2) {
        n2++;
        p2=p2.next;
    }
    if(n1>=n2) {
        while(pHead1) {
            pHead1.flag=1;
            pHead1=pHead1.next;
        }
        while(pHead2) {
            if(pHead2.flag===1) {
                return pHead2;
            }
            else {
                pHead2=pHead2.next;
            }
        }
    }
    else {
        while(pHead2) {
            pHead2.flag=1;
            pHead2=pHead2.next;
        }
        while(pHead1) {
            if(pHead1.flag===1) {
                return pHead1;
            }
            else {
                pHead1=pHead1.next;
            }
        }
    }
}
module.exports = {
    FindFirstCommonNode : FindFirstCommonNode
};

Guess you like

Origin blog.csdn.net/qq_43781887/article/details/128197901