Question: 3. Consider the LinkedList class with an inner Node class as following: public class LinkedList { private Node head; private class Node{ private T data;
3. Consider the LinkedList class with an inner Node class as following:
public class LinkedList
private Node head;
private class Node{
private T data;
private Node next;
public Node(T d) {
data = d;
next = null;
}
}
}
A linked list of integers created from the above List class is represented by the following diagram:

a. What is curNode.data after the following code segment is executed? (5 points)
Node curNode = head;
curNode = curNode.next.next;
b. What will be displayed after the following code segment is executed? (5 points)
Node curNode = head;
int n = 0;
while(curNode!= null){
n + = curNode.data;
curNode = curNode.next;
}
System.out.println(n= + n);
c. Draw a diagram of the above list after the following lines of code have been executed (5 points)
Node newNode = new Node(6);
Node curNode = head.next; newNode.next = curNode.next;
curNode.next = newNode;
d. In addition to the code above, assume the following code executes. Draw a diagram of the list after this code executes. (5 points)
Node curNode = head; curNode = curNode.next;
curNode = curNode.next;
Node nextNode= curNode.next;
curNode.next = nextNode.next;
nextNode = null;
7 3 4 8 head
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
