< Summary

Information
Class: LeetCode.Algorithms.AddTwoNumbers.AddTwoNumbersIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddTwoNumbers\AddTwoNumbersIterative.cs
Line coverage
100%
Covered lines: 23
Uncovered lines: 0
Coverable lines: 23
Total lines: 55
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddTwoNumbers(...)100%1010100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddTwoNumbers\AddTwoNumbersIterative.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12using LeetCode.Core.Models;
 13
 14namespace LeetCode.Algorithms.AddTwoNumbers;
 15
 16/// <inheritdoc />
 17public class AddTwoNumbersIterative : IAddTwoNumbers
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n + m), where n is the length of the list l1 and m is the length of the list l2
 21    ///     Space complexity - O(max(n, m)), where n is the length of the list l1 and m is the length of the list l2
 22    /// </summary>
 23    /// <param name="l1"></param>
 24    /// <param name="l2"></param>
 25    /// <returns></returns>
 26    public ListNode? AddTwoNumbers(ListNode? l1, ListNode? l2)
 427    {
 428        var dummyHead = new ListNode();
 429        var current = dummyHead;
 430        var carry = 0;
 31
 2732        while (l1 != null || l2 != null || carry != 0)
 2333        {
 2334            var sum = carry;
 35
 2336            if (l1 != null)
 1237            {
 1238                sum += l1.val;
 1239                l1 = l1.next;
 1240            }
 41
 2342            if (l2 != null)
 1843            {
 1844                sum += l2.val;
 1845                l2 = l2.next;
 1846            }
 47
 2348            carry = sum / 10;
 2349            current.next = new ListNode(sum % 10);
 2350            current = current.next;
 2351        }
 52
 453        return dummyHead.next;
 454    }
 55}