It is necessary to read two lists from two files. And create a third list that contains elements that are in the first, but not in the second and which are in the second, but not in the first list. Here is my code, although I have so far just tried to bring these elements to the console, and not insert them into the list. I can not understand what the error is.
public class Element { public int value; public Element next; } public static void main(String[] args) throws FileNotFoundException { Element head1 = null, head2 = null, temp1 = null, temp2 = null; Scanner sc = new Scanner(new File("C://Users//1//Desktop//12w.txt")); if (sc.hasNextInt()) { head1 = new Element(); //голова списка int m = sc.nextInt(); head1.value = m; // вставляю первый элемент в голову temp1 = head1; // указатель, который на данный момент указывает на голову while (sc.hasNextInt()) { // заполняю список m = sc.nextInt(); temp1.next = new Element(); temp1 = temp1.next; // перемещаю на следующее место temp1.value = m; } } sc = new Scanner(new File("C://Users//1//Desktop//12.txt")); // аналогично заполняю второй список if (sc.hasNextInt()) { head2 = new Element(); int m = sc.nextInt(); head2.value = m; temp2 = head2; while (sc.hasNextInt()) { m = sc.nextInt(); temp2.next = new Element(); temp2 = temp2.next; temp2.value = m; } } int s; int m1; int m2; temp1 = head1; //cсылаю указатель на голову while (temp1.next != null) { m1 = temp1.value; // делаю до тех пор пока указатель не ссылается на null s = 1; while (temp2.next != null) { m2 = temp2.value; // аналогично if (m1 == m2) { s = 0; break; // если два элемента при первом проходе равны } temp2 = temp2.next; // ссылаю указатель на след элемент } temp1 = temp1.next; if (s == 1) { System.out.println(temp1.value); } temp2 = head2; // указатель ссылаю на голову } }