func SumLinkedLists(l1, l2 *linkedlist.LinkedList) *linkedlist.LinkedList { sum := linkedlist.New() carry := 0 cur1, cur2 := l1.Root, l2.Root for cur1 != nil && cur2 != nil { total := cur1.Value + cur2.Value + carry val := total % 10 carry = total / 10 sum.Add(val) cur1 = cur1.Next cur2 = cur2.Next } if cur1 != nil { sum.Add(cur1.Value + carry) } else if cur2 != nil { sum.Add(cur2.Value + carry) } else if carry > 0 { sum.Add(carry) } return sum }
func edgeCases(t *testing.T, testFunc q02Function) { l := linkedlist.New() n := testFunc(l, 1) if n != nil { t.Errorf("Failure with out of bounds K, zero length") } l.Add(1) n = testFunc(l, 2) if n != nil { t.Errorf("Failure with out of bounds K, non-zero length") } n = testFunc(l, 0) if n != nil { t.Errorf("Failure with invalid K of 0") } n = testFunc(l, -1) if n != nil { t.Errorf("Failure with negative K") } }