链表-打印两个有序链表的公共部分

//打印两个有序链表的公共部分
public class Node{
    public int value;
    public Node next;
    public Node(int data){
        this.data=data;
    }
}
public void printCommonPart(Node head1,Node head2){
    System.out.println("Common Part:");
    while(head1!=null&&head2!=null){
        if(head1.value<head2.value){
            head1=head1.next;
        }else if(head1.value>head2.value){
            head2=head2.next;
        }else{
            System.out.println(head1.value+" ");
            head1=head1.next;
            head2=head2.next;
        }
    }
    System.out.println();
}

猜你喜欢

转载自blog.csdn.net/weixin_42146769/article/details/88381726