...

/

Solution Review: Pairs with Sums

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.head
q = None
while p:
q = p.next
while q:
if p.data + q.data == sum_val:
pairs.append("(" + str(p.data) + "," + str(q.data) + ")")
q = q.next
p = p.next
return 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 ...