Question: Given the ListNode definition as seen: public class ListNode { public int data; // data stored in this node public ListNode next; // link to
Given the ListNode definition as seen:
public class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list } And the LinkedIntList definition, including a print method which prints the elements starting at front until the end is reached:
public class LinkedIntList { private ListNode front; public void print() { ListNode n = front; while (n != null) { System.out.print(n.data+" "); n = n.next; } System.out.println(); } } Assume you have a LinkedIntList a which would be printed in the following way:
1 2 3 4 5
Consider the method mystery1 added to the LinkedIntList class above:
public void mystery1() { for (ListNode node = front; node != null; node = node.next) { if (node.next != null) { node.next = node.next.next; } } } Suppose the following code is run:
LinkedIntList a = ...; // initialized as described above to 1 2 3 4 5 a.mystery1(); a.print();
What would be printed out by the last line?
| 2 4 |
| 2 3 4 5 |
| 1 5 |
| 1 3 5 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
