Java - the first common node of two linked lists

topic link

Niuke online oj question - the first common node of two linked lists

topic description

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 figure below: you
insert image description here
can see their first common node The node value of is 6, so return the node whose node value is 6.

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 the 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.

Topic example

Example 1

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

Return value:
{6,7}

Explanation:
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 It is the common part of the two linked lists.
These three parameters will be assembled into two two acyclic single linked lists in the background, and they have common nodes.

Example 2

Input:
{1},{2,3},{}

return value:
{}

Explanation:
The two linked lists have no common nodes, return null, and print {} in the background

problem solving ideas

When it comes to the problem that two linked lists have common nodes, the method of fast and slow pointers is generally adopted.
For this question, you can first find the lengths len1 and len2 of the two linked lists, and the fast pointer points to the head pointer with the longer length of the linked list, and then let the fast pointer take the Math.abs(len1, len2) step first, so that the fast pointer and the slow pointer The distance from the common node is the same length, the fast pointer and the slow pointer each take a step, as long as the fast pointer and the slow pointer point to the same node, it means that this node is the common node of the two linked lists

And if the two linked lists do not have a common node, then the final fast pointer will point to an empty node, so it is necessary to judge whether the fast and slow pointers are empty when the fast and slow pointers move, and return directly if they are empty

full code

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    
    
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
    
    
        int len1 = getListLen(pHead1);
        int len2 = getListLen(pHead2);
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;

        if(len1 > len2){
    
    
            int tmp = len1 - len2;
            while(tmp > 0){
    
    
                cur1 = cur1.next;
                tmp--;
            }
        } else {
    
    
            int tmp = len2 - len1;
            while(tmp > 0){
    
    
                cur2 = cur2.next;
                tmp--;
            }
        }

        while(cur1 != null && cur2 != null && cur1 != cur2){
    
    
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    private int getListLen(ListNode pHead1) {
    
    
        ListNode cur = pHead1;
        int len = 0;
        while(cur != null){
    
    
            cur = cur.next;
            len++;
        }
        return len;
    }
}

Guess you like

Origin blog.csdn.net/m0_60867520/article/details/130410274