打印两个有序链表的公共部分 【题目】 给定两个有序链表的头指针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 }

 

转载于:https://www.cnblogs.com/lihuazhu/p/10908648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值