< Summary

Information
Class: LeetCode.Algorithms.MergeTwoSortedLists.MergeTwoSortedListsLinear
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MergeTwoSortedLists\MergeTwoSortedListsLinear.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 61
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MergeTwoLists(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MergeTwoSortedLists\MergeTwoSortedListsLinear.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.MergeTwoSortedLists;
 15
 16/// <inheritdoc />
 17public class MergeTwoSortedListsLinear : IMergeTwoSortedLists
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n + m)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="list1"></param>
 24    /// <param name="list2"></param>
 25    /// <returns></returns>
 26    public ListNode? MergeTwoLists(ListNode? list1, ListNode? list2)
 527    {
 528        if (list1 == null)
 229        {
 230            return list2;
 31        }
 32
 333        if (list2 == null)
 134        {
 135            return list1;
 36        }
 37
 238        ListNode dummyHead = new();
 239        var current = dummyHead;
 40
 941        while (list1 != null && list2 != null)
 742        {
 743            if (list1.val < list2.val)
 444            {
 445                current.next = list1;
 446                list1 = list1.next;
 447            }
 48            else
 349            {
 350                current.next = list2;
 351                list2 = list2.next;
 352            }
 53
 754            current = current.next;
 755        }
 56
 257        current.next = list1 ?? list2;
 58
 259        return dummyHead.next;
 560    }
 61}