Given the source code of linked List, answer the below questions(image): A. Fill out the method printList that print all the values of the linkedList: Draw the linked list. public void printList() { } // End of print method B. Write the lines to insert 10 at the end of the linked list. You must draw the final linked List. Notice that you can’t use second or third nodes. Feel free to define a new node. Assume you have only a head node C. Write the lines to delete node 2. You must draw the final linked list. Notice that you can’t use second or third node. Feel free to define a new node. Assume you have only a head node

icon
Related questions
Question

Given the source code of linked List, answer the below questions(image):

A. Fill out the method printList that print all the values of the linkedList: Draw the linked list. public void printList() { } // End of print method

B. Write the lines to insert 10 at the end of the linked list. You must draw the final linked List. Notice that you can’t use second or third nodes. Feel free to define a new node. Assume you have only a head node

C. Write the lines to delete node 2. You must draw the final linked list. Notice that you can’t use second or third node. Feel free to define a new node. Assume you have only a head node

public class LinkedList {
Node head; // Head of list
/* Linked list Node. This inner class is made static so that
main() can access it */
static class Node {
int data;
Node next;
Node (int d)
{
data = d;
next = null;
} // Constructor
} // End of node
public static void main(String[] args) {
// Start with the empty list
LinkedList list = new LinkedList();
list.head = new Node (1);
Node second = new Node (2);
Node third = new Node (3);
list.head.next = second;
second.next = third;
list.printList();
} // End of main
}
Transcribed Image Text:public class LinkedList { Node head; // Head of list /* Linked list Node. This inner class is made static so that main() can access it */ static class Node { int data; Node next; Node (int d) { data = d; next = null; } // Constructor } // End of node public static void main(String[] args) { // Start with the empty list LinkedList list = new LinkedList(); list.head = new Node (1); Node second = new Node (2); Node third = new Node (3); list.head.next = second; second.next = third; list.printList(); } // End of main }
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 3 images

Blurred answer