I need a method for public void ******************
write the method “insertDoubles” that for each value found in an integer linked list it inserts that value doubled after the original. The method should return nothing and take in in no parameters. If the list is empty, then the method should do nothing. To avoid infinite loops you need to move the iterator past the newly inserted value.
here is the provided code
public class Question02 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public void insertDoubles() { //----------------------------------------------------------------------------------- //Write your solution here }//Do not alter this //-------------------------------------------------------------------------------- //Test your code here. public static void main(String[] args) { //Example Question02 intLL = new Question02(); intLL.head = intLL.new ListNode(0, intLL.new ListNode(1, intLL.new ListNode(2, intLL.new ListNode(3, intLL.new ListNode(4,null))))); intLL.insertDoubles(); //Printing Results for(Question02.ListNode temp = intLL.head;temp != null; temp = temp.link) System.out.println(temp.data); } //-------------------------------------------------------------------------------- }//Do not alter this
Solution Tests:
Code:
public void insertDoubles()
{
if (this.head == null){
return;
}
for(Question02.ListNode temp = this.head; temp != null;){
Question02.ListNode newLink = temp.link;
temp.link = new Question02.ListNode(temp.data*2, temp.link);
temp = newLink;
}
}
output snippet:
<0,1,2,3,4>
<2,4,6,8,10>
Empty list: <>
Get Answers For Free
Most questions answered within 1 hours.