< Summary

Information
Class: LeetCode.Algorithms.AddTwoNumbers.AddTwoNumbersInPlaceCarry
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddTwoNumbers\AddTwoNumbersInPlaceCarry.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 76
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
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%1616100%
Reverse(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\AddTwoNumbers\AddTwoNumbersInPlaceCarry.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 AddTwoNumbersInPlaceCarry : 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 carry = 0;
 29
 430        ListNode? head = null;
 31
 2532        while (l1 != null || l2 != null)
 2133        {
 2134            var val1 = l1?.val ?? 0;
 2135            var val2 = l2?.val ?? 0;
 36
 2137            var sum = val1 + val2 + carry;
 38
 2139            carry = 0;
 40
 2141            if (sum >= 10)
 1842            {
 1843                carry = 1;
 1844                sum -= 10;
 1845            }
 46
 2147            l1 = l1?.next;
 2148            l2 = l2?.next;
 49
 2150            head = new ListNode(sum, head);
 2151        }
 52
 453        if (carry > 0)
 254        {
 255            head = new ListNode(1, head);
 256        }
 57
 458        return Reverse(head);
 459    }
 60
 61    private static ListNode? Reverse(ListNode? head)
 462    {
 463        ListNode? prev = null;
 464        var current = head;
 65
 2766        while (current != null)
 2367        {
 2368            var next = current.next;
 2369            current.next = prev;
 2370            prev = current;
 2371            current = next;
 2372        }
 73
 474        return prev;
 475    }
 76}