[Algorithm] Prints the public part of two linked lists

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.