题目
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分。
代码
class Node {
int value;
Node next;
public Node(int value) {
this.value = value;
}
}
/**
* 打印两个有序链表的公共部分
*
* @param head1
* @param head2
*/
private static void printCommonPart(Node head1, Node head2) {
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.print(head1.value + "\t");
head1 = head1.next;
head2 = head2.next;
}
}
}