Imagine a non-ordered singularly-linked structure with integer nodes (ex. 5 -> 8 -> 10 -> 2 -> 7), and a given sum (ex. 15). How could I find the first subset to equate to the sum recursively? In this example, the answer would be 5 -> 8 -> 2 (5 -> 10 or 8 -> 7 would also be an answer, but not the first). I have a method "node f(node, sum)":
if node == null
return null
else if node.value <= target
return new Node(node.item, f(node.next, sum - node.value)
else
return f(node.next, sum)
However, this will return subsets that are less than or equal to the sum, not exactly the sum. How can I create an exact algorithm?