栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

左神面试指南——打印两个有序链表我的公共部分

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

左神面试指南——打印两个有序链表我的公共部分

题目描述:
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。
代码:

public class printCommonPart {

    public static void printCommonPart(Node head1,Node head2)
    {
        System.out.println("Common Part:");
        while (head1!=null&&head2!=null)
        {
            if(head1.value 

测试代码:

public class test {
    public static void main(String[] args) {
        printCommonPart test = new printCommonPart();
        Node head1 = new Node(0);
        Node head2 = new Node(2);
        Node cur = head1;
        for(int i = 1;i<10;i++)
        {
            Node temp = new Node(i);
            cur.next = temp;
            cur = temp;
        }
        Node cur1 = head2;
        for(int i = 3;i<15;i++)
        {
            Node temp = new Node(i);
            cur1.next = temp;
            cur1 = temp;
        }
        test.printCommonPart(head1,head2);
    }
}

结果输出:

说明:
前一段时间一直在刷LeetCode的每日一题,感觉知识点太琐碎。于是准备按题型开刷,找到了左神的书,写的非常好。每天学一点,加油!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/632595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号