查找两个有序链表的相同部分

/**
 * Created by 糖糖 on 2017/8/2.
 */
public class printCommonPart {
    public static void printCommonPart(node head1,node head2){
        while (head1!= null && head2!= null){
            if(head1.data>head2.data)
                head2 = head2.next;
            else if(head1.data<head2.data)
                head1 = head1.next;
            else {
                System.out.print(head1.data+" ");
                head1 = head1.next;
                head2 = head2.next;
            }
        }
    }
    public static  void  main(String args[]){
        node n1=new node(2);
        node n2=new node(5);
        node n3=new node(6);
        node n4=new node(7);
        n1.next=n2;
        n2.next=n3;
        n3.next=n4;
        node n5=new node(5);
        node n6=new node(6);
        node n7=new node(7);
        n5.next=n6;
        n6.next=n7;
        printCommonPart(n1,n5);
    }
}
class node{
    public int data;
    public  node next;
    public node(int data){
        this.data = data;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值