< Summary

Information
Class: LeetCode.Algorithms.DoubleNumberRepresentedAsLinkedList.DoubleNumberRepresentedAsLinkedListIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DoubleNumberRepresentedAsLinkedList\DoubleNumberRepresentedAsLinkedListIterative.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 85
Line coverage: 100%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DoubleIt(...)91.66%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DoubleNumberRepresentedAsLinkedList\DoubleNumberRepresentedAsLinkedListIterative.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.DoubleNumberRepresentedAsLinkedList;
 15
 16/// <inheritdoc />
 17public class DoubleNumberRepresentedAsLinkedListIterative : IDoubleNumberRepresentedAsLinkedList
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="head"></param>
 24    /// <returns></returns>
 25    public ListNode? DoubleIt(ListNode? head)
 226    {
 227        ListNode? previous = null;
 28
 229        var current = head;
 30
 831        while (current != null)
 632        {
 633            var next = current.next;
 34
 635            current.next = previous;
 636            previous = current;
 637            current = next;
 638        }
 39
 240        var reversedHead = previous;
 41
 242        var node = reversedHead;
 243        var carry = 0;
 44
 245        ListNode? lastNode = null;
 46
 847        while (node != null)
 648        {
 649            var newVal = (node.val * 2) + carry;
 50
 651            if (newVal > 9)
 552            {
 553                carry = 1;
 54
 555                newVal %= 10;
 556            }
 57            else
 158            {
 159                carry = 0;
 160            }
 61
 662            node.val = newVal;
 663            lastNode = node;
 664            node = node.next;
 665        }
 66
 267        if (lastNode != null && carry > 0)
 168        {
 169            lastNode.next = new ListNode(carry);
 170        }
 71
 272        previous = null;
 273        current = reversedHead;
 74
 975        while (current != null)
 776        {
 777            var next = current.next;
 778            current.next = previous;
 779            previous = current;
 780            current = next;
 781        }
 82
 283        return previous;
 284    }
 85}