package com.camnter.basicexercises.linklist;
import com.camnter.basicexercises.core.Node;
/**
* 打印两个有序链表的公共部分
*
* head1 < head2,head1 往前走
* head1 > head2,head2 往前走
* head1 = head2,一起走
*
* 小的走,同则一起走
*
* @author CaMnter
*/
public class PrintLinkListCommonPart> {
void printLinkListCommonPart(Node head1, Node head2) {
while (head1 != null && head2 != null) {
if (head1.value.compareTo(head2.value) < 0) {
head1 = head1.next;
} else if (head1.value.compareTo(head2.value) > 0) {
head2 = head2.next;
} else {
// ==
System.out.print(head1.value + " ");
head1 = head1.next;
head2 = head2.next;
}
}
System.out.println(" ");
}
public static void main(String[] args) {
// 1 2 3 4 5 6
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
// 3 4 5 6 7 8
Node head2 = new Node(3);
head2.next = new Node(4);
head2.next.next = new Node(5);
head2.next.next.next = new Node(6);
head2.next.next.next.next = new Node(7);
head2.next.next.next.next.next = new Node(8);
PrintLinkListCommonPart printLinkListCommonPart = new PrintLinkListCommonPart();
printLinkListCommonPart.printLinkListCommonPart(head1, head2);
}
}