Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

intro to java, problem help please 4. A linked list of integers is shown below.

ID: 3919475 • Letter: I

Question

intro to java, problem help please

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

a. Output: ----------- 3 b.Output: ----------- after the code is run, first variable is set to null. which is the unintended side affect. modified code: --------------- int sum = 0; Node temp = first; while(temp != null) { sum += first.data; temp = temp.next; } System.out.println(sum);