public class SingleLL
extends java.lang.Object
You need to complete this class by adding methods to insert, remove, and swap values. Note that our test suites walks this list internally, so you need to maintain the start reference and the structure of the Node class for the tests to pass.
Like trees, linked lists are also recursive data structure, since each part of the list is itself a list. However, linked lists can be manipulated easily using both recursive and iterative algorithms. We leave the choice to you.
Modifier and Type | Class | Description |
---|---|---|
class |
SingleLL.Node |
Internal class storing a node in our SingleLL.
|
Modifier and Type | Method | Description |
---|---|---|
SingleLL.Node |
getStart() |
Get a reference to the start of the list.
|
boolean |
insert(int newValue,
int position) |
Insert a new value into the linked list.
|
boolean |
remove(int position) |
Remove a value from the linked list.
|
boolean |
swap(int firstPosition,
int secondPosition) |
Swap two values in the list.
|
java.lang.String |
toString() |
|
void |
unshift(int newValue) |
Add a new value to the front of the list.
|
public SingleLL.Node getStart()
Do not remove or break this method. The testing suite uses it to do its own testing.
public void unshift(int newValue)
Do not remove or break this method. The testing suite uses it to do its own testing.
newValue
- the new int value to add to the front of the listpublic boolean insert(int newValue, int position)
Returns true if the insert succeeded, and false if the position was either less than zero or off the end of the list.
newValue
- the new value to insertposition
- the position to insert the value atpublic boolean remove(int position)
Returns true if the removals succeeded, and false if the position was either less than zero or off the end of the list.
position
- the position to removepublic boolean swap(int firstPosition, int secondPosition)
Returns true if the swap succeeded, and false if the either position was either less than zero or off the end of the list.
firstPosition
- the first value to swapsecondPosition
- the second value to swappublic java.lang.String toString()
toString
in class java.lang.Object