// file TestEqualList2 // Singly linked list // import java.util.*; class IntList2 { private int value; private IntList2 next; public IntList2(int v, IntList2 n) { value = v; next=n; } public boolean equals(IntList2 m) { IntList2 l = this; while (l != null && m != null) { if (l.value != m.value) return false; l=l.next; m=m.next; } return ((l == null) && (m == null)); } // end equals } // end IntList2 public class TestEqualLists2 { public static void main(String[] args) { IntList2 n2 = new IntList2(12,null); IntList2 n1 = new IntList2(9,n2); IntList2 m2 = new IntList2(12,null); IntList2 m1 = new IntList2(9,m2); IntList2 q1 = m1; System.out.println("m1.equals(n1): " + m1.equals(n1)); System.out.println("m1.equals(q1): " + m1.equals(q1)); System.out.println("m1 == n1: " + (m1 == n1)); System.out.println("m1 == q1: " + (m1 == q1)); } }