Topic:
The Head1 and head2 of the heads of the two ordered linked lists print the public parts of the two linked lists.
Answer:
Since there is a list of sequences, the following judgments are made from the heads of the two linked lists:
- If the value of Head1 is less than head2, then head1 moves down.
- If the value of HEAD1 is greater than head2, then head2 moves down.
- If the values are equal, the value is printed, and both Head1 and head2 move down.
- Head1 and head2 have any one move to NULL, the entire process stops.
Program:
Public Static class Node {
Public int value;
Public Node Next;
Public Node (int data) {
this. Value = data;
}
}
Public Static void Printcommonpart (node head1, node head2) {
System.out.print ("");
while NULL null) {
if (Head1.value < Head2.value) {
Head1 = Head1.next;
Else if (Head1.value > Head2.value) {
Head2 = Head2.next;
Else {
System.out.print (Head1.value + "");
Head1 = Head1.next;
Head2 = Head2.next;
}
}
System.out.println ();
}
Public Static void printlinkedlist (node node) {
System.out.print ("");
while null) {
System.out.print (Node.value + "");
node = Node.next;
}
System.out.println ();
}
[Algorithm] Prints the public part of two linked lists