Given a linked list of integers in random order, divide it into two new linked lists so that the difference in the sum of the elements of each list is maximum and the length of the lists differs by no more than 1 (in the case that the original list contains an odd number of elements). I cannot assume that the numbers on the list are unique.
The algorithm that I was thinking about was to sort the merge in the original linked list (time O (n · log n), O (n)), and then use the recursive function to go to the end of the list to determine its length while doing the splitting while the recursive function is unwound. A recursive function is O (n) time and O (n) space.
Is this the best solution? I can post my code if someone considers it appropriate.
source
share