打印有序链表的公共部分


import java.util.LinkedList;
import java.util.List;

public class Node {
    
    public static void printCommonPart(List<Integer> node1,List<Integer> node2) {
        System.out.println("common Part: ");
        for(int i=0,j=0;i<node1.size() &&j<node2.size();) {
            System.out.print(i+":");
            if(node1.get(i)<node2.get(j)) {
                i++;
            }else if (node1.get(i)<node2.get(j)) {
                j++;
            }else {
                System.out.println(node1.get(i) +" ");
                i++;
                j++;
            }
        }
    }
    
    public static void main(String args[]) {
        List<Integer> node2 = new LinkedList<>();
        List<Integer> node1 = new LinkedList<>();
        node1.add(1);
        node1.add(3);
        node1.add(6);
        node1.add(81);
        node1.add(432);
        
        node2.add(2);
        node2.add(3);
        node2.add(11);
        node2.add(61);
        node2.add(81);
        node2.add(432);
        
        printCommonPart(node1,node2);
    }
    
}

原文地址:https://www.cnblogs.com/interfaceone/p/7739202.html