< Summary

Information
Class: LeetCode.Algorithms.SplitLinkedListInParts.SplitLinkedListInPartsIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SplitLinkedListInParts\SplitLinkedListInPartsIterative.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 80
Line coverage: 100%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SplitListToParts(...)88.88%1818100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SplitLinkedListInParts\SplitLinkedListInPartsIterative.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.SplitLinkedListInParts;
 15
 16/// <inheritdoc />
 17public class SplitLinkedListInPartsIterative : ISplitLinkedListInParts
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="head"></param>
 24    /// <param name="k"></param>
 25    /// <returns></returns>
 26    public ListNode?[] SplitListToParts(ListNode? head, int k)
 327    {
 328        if (head == null)
 129        {
 130            return new ListNode[k];
 31        }
 32
 233        var length = 0;
 234        var currentNode = head;
 35
 1536        while (currentNode != null)
 1337        {
 1338            length++;
 39
 1340            currentNode = currentNode.next;
 1341        }
 42
 243        var partSize = length / k;
 244        var extraNodesCount = length % k;
 45
 246        var result = new ListNode?[k];
 47
 248        currentNode = head;
 49
 1650        for (var i = 0; i < k && currentNode != null; i++)
 651        {
 652            result[i] = currentNode;
 53
 654            var currentPartSize = partSize;
 55
 656            if (extraNodesCount > 0)
 457            {
 458                currentPartSize++;
 59
 460                extraNodesCount--;
 461            }
 62
 2663            for (var j = 0; j < currentPartSize - 1; j++)
 764            {
 765                currentNode = currentNode?.next;
 766            }
 67
 668            var nextPart = currentNode?.next;
 69
 670            if (currentNode != null)
 671            {
 672                currentNode.next = null;
 673            }
 74
 675            currentNode = nextPart;
 676        }
 77
 278        return result;
 379    }
 80}