Question: package Exercise2; public class SinglyLinkedList { private class Node { int data; Node next; } private Node head; public void add(int item) { Node node

package Exercise2;

public class SinglyLinkedList {

private class Node {

int data;

Node next;

}

private Node head;

public void add(int item) {

Node node = new Node();

node.data = item;

node.next = head;

head = node;

}

public void concatenate(SinglyLinkedList other) {

Node current = head;

while (current.next != null) {

current = current.next;

}

current.next = other.head;

}

public void print() {

Node current = head;

while (current != null) {

System.out.print(current.data + " ");

current = current.next;

}

System.out.println();

}

public static void main(String[] args) {

SinglyLinkedList list1 = new SinglyLinkedList();

list1.add(1);

list1.add(2);

list1.add(3);

list1.print();

SinglyLinkedList list2 = new SinglyLinkedList();

list2.add(4);

list2.add(5);

list2.add(6);

list2.print();

list1.concatenate(list2);

list1.print();

}

}

Please add comments to my code. Like //this function does this

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!