logo Practice-It logo

BJP5 Exercise 16.21: surroundWith

Related Links:
Author: Marty Stepp (on 2019/09/19)

Write a method called surroundWith that takes an integer x and an integer y as parameters and surrounds all nodes in the list containing the value x with new nodes containing the value y. In particular, each node that contains the value x as data should have a new node just before it and just after it that each contain the value y. If no nodes in the list contain the value x, then the list should not be modified. For example, suppose that the variables list1, list2, and list3 store the following sequences of values:

[0, 1, 2, 1]    // stored in list1
[0, 1, 0]       // stored in list2
[0, 1, 2]       // stored in list3

and we make the following calls:

list1.surroundWith(1, 4);   // surround 1s with 4s
list2.surroundWith(1, 1);   // surround 1s with 1s
list3.surroundWith(3, 4);   // surround 3s with 4s

then the variables will now store these sequences:

[0, 4, 1, 4, 2, 4, 1, 4]    // stored in list1
[0, 1, 1, 1, 0]             // stored in list2
[0, 1, 2]                   // stored in list3

Assume that you are adding this method to the LinkedIntList class as defined below:

public class LinkedIntList {
    private ListNode front;   // null for an empty list
Type your solution here:

This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.