Intro to java LinkedList 4. A linked list of integers is shown below. a. What is
ID: 3920516 • Letter: I
Question
Intro to java LinkedList
4. A linked list of integers is shown below. a. What is the output for the following code in general? What should be added to the code to avoid a runtime error? Node pfirst: while (p.next- null)( P P.next System.out.println (p.data) b. The output of the following code is the sum of all data values in the linked list. Explain the unintended side effect that occurs when this code is run. Rewrite the code to avoid this side effect int sum0 while (first null) sum first.data; firstfirst.next; System.out.println (s)Explanation / Answer
4a. Output: 3
Explanation: It loops as long as there is data in linked list, when it reaches end it comes out of the loop to print the data in the last link
Modification: Condition in while must be changed to below
while(p != null)
4b. In the last line s is used instead of sum.
System.out.println(sum)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.