• 打印两个有序链表的公共部分 【题目】 给定两个有序链表的头指针head1和head2,打印两个 链表的公共部分


    简单题

     1 package my_basic.class_3;
     2 
     3 public class Code_10_PrintCommonPart {
     4     
     5     public static class Node{
     6         int value;
     7         Node next;
     8         public Node(int value) {
     9             super();
    10             this.value = value;
    11         }
    12     }
    13     
    14     public static void printCommonPart(Node head1,Node head2) {
    15         System.out.println("common part:");
    16         while(head1!=null && head2!=null) {
    17             if (head1.value > head2.value) {
    18                 head2 = head2.next;
    19             }else if (head1.value < head2.value) {
    20                 head1 = head1.next;
    21             }else {
    22                 System.out.print(head1.value+" ");
    23                 head1 = head1.next;
    24                 head2 = head2.next;
    25             }
    26         }
    27         System.out.println();
    28     }
    29     
    30     public static void printLinkedList(Node head) {
    31         while(head!=null) {
    32             System.out.print(head.value+" ");
    33             head = head.next;
    34         }
    35         System.out.println();
    36     }
    37     
    38     public static void main(String[] args) {
    39         Node node1 = new Node(2);
    40         node1.next = new Node(3);
    41         node1.next.next = new Node(5);
    42         node1.next.next.next = new Node(6);
    43 
    44         Node node2 = new Node(1);
    45         node2.next = new Node(2);
    46         node2.next.next = new Node(5);
    47         node2.next.next.next = new Node(7);
    48         node2.next.next.next.next = new Node(8);
    49 
    50         printLinkedList(node1);
    51         printLinkedList(node2);
    52         printCommonPart(node1, node2);
    53 //        System.out.println(node1.value);
    54     }
    55 }
  • 相关阅读:
    PIE-Basic 自定义滤波
    PIE-Basic 常用滤波
    PIE-Basic 中值滤波
    PIE-Basic 均值滤波
    PIE-Basic 小波变换
    PIE-Basic 傅里叶变换
    PIE-Basic 去相关拉伸
    .net调用数据库执行Mysql存储过程,提示 Procedure or function XXXX Cannot be found in database xxx
    C#几种类型转换方法的个人总结
    Winform无法改变窗体大小
  • 原文地址:https://www.cnblogs.com/lihuazhu/p/10908648.html
Copyright © 2020-2023  润新知