题解 | 链表相交

链表相交

https://www.nowcoder.com/practice/bd911c77a1ed4e289a0699fa7df23b6c

import java.util.*;

public class Main {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // 在这里补充代码
        // ListNode ptrA = headA;
        // ListNode ptrB = headB;
        // // 当两个指针不相遇时继续循环
        // while (ptrA != ptrB) {
        //     // 如果走到链表末尾,就切换到另一个链表头部
        //     ptrA = (ptrA == null) ? headB : ptrA.next;
        //     ptrB = (ptrB == null) ? headA : ptrB.next;
        // }
        // return ptrA; // 返回相交节点或null


        ListNode nodeA = headA;
        ListNode nodeB = headB;
        while (nodeA != null) {
            while (nodeB != null) {
                if (nodeA == nodeB) {
                    return nodeA;
                }
                nodeB = nodeB.next;
            }
            nodeB = headB;
            nodeA = nodeA.next;
        }
        return null;

    }




























    //你不需要关心主函数的内容!
    public static void main(String[] args) {
        Main solution = new Main();
        Scanner scanner = new Scanner(System.in);

        // 读入数据
        int lenA = scanner.nextInt();
        int lenB = scanner.nextInt();
        int commonLen = scanner.nextInt();

        // 构建链表
        ArrayList<ListNode> nodesA = new ArrayList<>();
        ArrayList<ListNode> nodesB = new ArrayList<>();
        ArrayList<ListNode> nodesCommon = new ArrayList<>();

        // 读入并创建链表A的独立部分
        for (int i = 0; i < lenA - commonLen; i++) {
            int val = scanner.nextInt();
            nodesA.add(new ListNode(val));
            if (i > 0) nodesA.get(i - 1).next = nodesA.get(i);
        }

        // 读入并创建链表B的独立部分
        for (int i = 0; i < lenB - commonLen; i++) {
            int val = scanner.nextInt();
            nodesB.add(new ListNode(val));
            if (i > 0) nodesB.get(i - 1).next = nodesB.get(i);
        }

        // 读入并创建公共部分
        for (int i = 0; i < commonLen; i++) {
            int val = scanner.nextInt();
            nodesCommon.add(new ListNode(val));
            if (i > 0) nodesCommon.get(i - 1).next = nodesCommon.get(i);
        }

        // 连接链表
        ListNode headA = null;
        ListNode headB = null;

        if (lenA - commonLen > 0) {
            headA = nodesA.get(0);
            if (commonLen > 0) nodesA.get(nodesA.size() - 1).next = nodesCommon.get(0);
        } else if (commonLen > 0) {
            headA = nodesCommon.get(0);
        }

        if (lenB - commonLen > 0) {
            headB = nodesB.get(0);
            if (commonLen > 0) nodesB.get(nodesB.size() - 1).next = nodesCommon.get(0);
        } else if (commonLen > 0) {
            headB = nodesCommon.get(0);
        }

        // 调用函数获取结果
        ListNode result = solution.getIntersectionNode(headA, headB);

        // 输出结果
        if (result == null) {
            System.out.println("null");
        } else {
            System.out.println(result.val);
        }

        scanner.close();
    }
}

全部评论

相关推荐

03-04 07:14
门头沟学院 C++
后测速成辅导一两个月...:老板:都给工作机会了还想要工资,哪来这么多好事
点赞 评论 收藏
分享
书海为家:实习是成为大厂正式员工很好的敲门砖,看您的简历中有一段实习经历,挺好的。我来给一点点小建议,因为毕竟还在学校不像工作几年的老鸟有丰富的项目经验,面试官在面试在校生的时候更关注咱们同学的做事逻辑和思路,所以最好在简历中描述下自己实习时做过项目的完整过程,比如需求怎么来的,你对需求的解读,你想到的解决办法,遇到困难如何找人求助,最终项目做成了什么程度,你从中收获了哪些技能,你有什么感悟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务