In this problem, you will complete the implementation of the addlast method in a single linked list. The add last method should add a new node to the end of the linked list Do not modify the list to be doubly linked. Remember that lists can be in two states: empty or with some elements (1 or more) The P1 class will run the test cases by reading inputs from the user and calling the addLast method. It will print the list after each addLast call Example Input Output Jane -> Jane -> Mike -> Jane -> Mike -> Bob -> 3 Jane Mike Bob
Java. Refer to screenshot. There is starter code. I need help with the method.
public class LinkedList {
Node head;
Node tail;
protected class Node{
String data;
Node next;
}
public void addLast(String s){
//Complete the method here
}
public String toString(){
String result = "";
Node n = head;
while(n != null){
result += n.data + " -> ";
n = n.next;
}
return result;
}
}
//DO NOT MODIFY THIS CLASS
//All work must be done in the LinkedList.java class
import java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int numNames = kb.nextInt();
LinkedList list = new LinkedList();
for (int i = 0; i < numNames ; i++) {
list.addLast(kb.next());
System.out.println(list);
}
}
}


Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 3 images









