Write the methods below as extensions to the LinkedList class.
Extend the linked list with these three different methods:
.append(value)
(adds a new node with the givenvalue
to the end of the list)
Input: a stringvalue
Output: a node with thevalue
inserted at the end of the list
Edge Case(s):
- If linked list is empty (head = null), the new node should be the head
.insertBefore(value, newVal)
(adds a new node withnewVal
immediately before the first node containingvalue
)
Input: avalue
string to search for, anewVal
string to insert in a new node beforevalue
Output: a node with thenewVal
inserted before the searchedvalue
Edge Case(s):
- Attempting to insertBefore() the head node should divert to insert() instead.
.insertAfter(value, newVal)
(adds a new node withnewVal
immediately after the first node containingvalue
)
Input: avalue
string to search for, anewVal
string to insert in a new node aftervalue
Output: a node with thenewVal
inserted after the searchedvalue
Edge case(s):
- Attempting to insertAfter() the tail node should divert to append() instead.
Write tests to prove the following functionality: