Count Pairs With a Target Sum
Count the pairs of different nodes (positions i < j) whose values add up to target. Two equal values at different positions form a pair. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [1,2,3,4]
target 5
returns 2
1+4 and 2+3.
Example 2
head [2,2,2]
target 4
returns 3
Three pairs of positions.
Constraints
0 <= number of nodes <= 100-100 <= Node.val <= 100-200 <= target <= 200
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.