In the text we developed Java code for traversing a linked list. Here is a, possibly flawed, approach for using a traversal to print the contents of the linked list of strings accessed through letters. Indicate the description that best matches the code.
LLNode<String> currNode = letters;
while (currNode != null)
{
System.out.println(currNode.getInfo());
if (currNode.getLink() != null)
currNode = currNode.getLink();
else
currNode = null;
}
Given code is LLNode<String> currNode = letters; while (currNode != null) { System.out.println(currNode.getInfo()); if (currNode.getLink() != null) currNode = currNode.getLink(); else currNode = null; } There is no flaw in the given code. It is correct but not efficient. Because There is a if condition checking for currNode.getLink() is null or not. If it is null then going to else case and assigning null. In the case of currNode is null Both currNode = currNode.getLink(); and currNode = null; does same thing. So, we can write same code as LLNode<String> currNode = letters; while (currNode != null) { System.out.println(currNode.getInfo()); currNode = currNode.getLink(); }
Get Answers For Free
Most questions answered within 1 hours.