Write a method calledCompareAndChangeList to be included in an application class calledListApplication that accepts two parameterslist1andlist2 of typeSingleLinkedList of integer values, and a third parameterItem of typeint. Iflist1 andlist2 are of different sizes, the method returns false, otherwise, the method replaces any element in list2 with a value equal to the last element in list1, if the summation of the element in list 1 and the corresponding element in list2 is equal toitem. Assume list1 and list2 are not empty. If at least one replacement happened, the method returns true, otherwise, it returns false.
Note: Write this method by calling methods of the classSingleLinkedList.
Method head:
public staticbooleanCompareAndChangeList (SingleLinkedList list1, SingleLinkedList list2, int item)Example:Before Method call:item = 35list1: 23 26 30 20 17list2: 10 9 25 15 4After Method call:list1: 23 26 30 20 17list2: 1017 2517 4In this case, the method will returntrueNote: as you can see the summation of 26 (in list 1) and its corresponding element (in list2) 9, is 35, which is equal toitem, so we replace the element in list 2 (9) by the value of the last data in list1 which is 17. The same thing is between the elements 20 (in list 1) and 15 (in list 2). 15 is going to be replaced by 17. The rest will not change.
Example:
Before Method call:item = 35
list1: 23 26 30 20 17
list2: 10 9 25 15 4
After Method call:
list2: 1017 2517 4
In this case, the method will returntrue
Note: as you can see the summation of 26 (in list 1) and its corresponding element (in list2) 9, is 35, which is equal toitem, so we replace the element in list 2 (9) by the value of the last data in list1 which is 17. The same thing is between the elements 20 (in list 1) and 15 (in list 2). 15 is going to be replaced by 17. The rest will not change.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here