Question
Write a new instance method for the linked list implementation of the MyLinkedList class called switchFirstWithLast. SwitchFirstWithLast switches the first node of a list with
Write a new instance method for the linked list implementation of the MyLinkedList class called switchFirstWithLast. SwitchFirstWithLast switches the first node of a list with the last node of another list. For example, if list1 contains the elements a, c, d, r, t and list2 contains: *, &, 6, x, b, then the call
list1.switchFirstWithLast (list2);
would leave list1 with the elements b, c, d, r, t
and list2 with the elements *, &, 6, x, a
Write the implementation code for the method SwitchFirstWithLast taking into consideration that MyLinkedList class have nodes which are instances of the following class:
class Node{
public Object data;
public Node next;
public Node(Object o){
data = o;
}
}
And MyLinkedList class have a public head attribute which is a reference to the first node and a public size attribute which is a count of nodes in the linked list and the following methods are already there:
- Two contructors
- boolean add(Object o, int index) // adds a node at a specific index. If index <0 or index> size the method returns false and returns true otherwise.
- boolean remove(Object o)// removes the first existence of the node containing data equal to o. if no node contains a value equal to o the method returns false.
Important Notes:
- you are not allowed to define any helper methods.
- MyLinkedList class does not have a tail attribute (reference to the last node).
in java
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started