Skip to content
Advertisement

Comparing two linked lists and returing a list with maximum values

I am planning to write a Java Function that takes two linked lists. Both have the same size. I want to return a new list that contains the maximum of the data found in the corresponding nodes of the two lists passed to my function.

However I am stuck in filling the new list. I came up with this:

JavaScript

I don’t know how to continue. I want list3 to contain the maximum values from the two lists.

Advertisement

Answer

Firstly, what you have written is not valid Java. Generics cannot use primitive types, such as the use of <int> in your example. It needs to be a class e.g. <Integer>. function is also not a keyword.

For brevity, the following code assumes both lists are of equal size:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement