Solution Review: Pairs with Sums
This lesson contains the solution review for the challenge of finding pairs in a doubly linked list which sum up to the given number.
We'll cover the following...
Here is an overview of our solution to finding a pair of nodes in a doubly linked list that sums up to a specific number.
Implementation
Here is the coding solution in Python for the previous challenge:
Press + to interact
def pairs_with_sum(self, sum_val):pairs = list()p = self.headq = Nonewhile p:q = p.nextwhile q:if p.data + q.data == sum_val:pairs.append("(" + str(p.data) + "," + str(q.data) + ")")q = q.nextp = p.nextreturn pairs
Explanation
On line 2, pairs
is initialized to an empty Python list. In the next lines (lines 3-4), p
and q
are set equal to self.head
...