< Summary

Information
Class: LeetCode.Core.Models.ListNode
Assembly: LeetCode.Core
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode.Core\Models\ListNode.cs
Line coverage
100%
Covered lines: 35
Uncovered lines: 0
Coverable lines: 35
Total lines: 76
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ToListNodeOrThrow(...)50%22100%
ToListNode(...)100%11100%
ToCycledListNode(...)100%1212100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode.Core\Models\ListNode.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 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
 12// ReSharper disable InconsistentNaming
 13
 14using LeetCode.Core.Exceptions;
 15
 16namespace LeetCode.Core.Models;
 17
 18/// <summary>
 19///     Definition for singly-linked list
 20/// </summary>
 21public sealed class ListNode
 22{
 23    public ListNode? next;
 24
 25    public int val;
 26
 130927    public ListNode(int val = 0, ListNode? next = null)
 130928    {
 130929        this.next = next;
 130930        this.val = val;
 130931    }
 32
 33    public static ListNode ToListNodeOrThrow(int[] array)
 3334    {
 3335        return ToListNode(array) ?? throw new ListNodeBuildException();
 3336    }
 37
 38    public static ListNode? ToListNode(int[] array)
 35339    {
 151640        return array.Reverse().Aggregate<int, ListNode?>(null, (next, val) => new ListNode(val, next));
 35341    }
 42
 43    public static ListNode? ToCycledListNode(int[] array, int cyclePosition)
 1844    {
 1845        var head = ToListNode(array);
 46
 1847        if (head == null || cyclePosition < 0)
 648        {
 649            return head;
 50        }
 51
 2452        ListNode? lastNode = head, cycleNode = null;
 53
 14054        for (var index = 0; lastNode.next != null; index++)
 5855        {
 5856            if (index == cyclePosition)
 1057            {
 1058                cycleNode = lastNode;
 1059            }
 60
 5861            lastNode = lastNode.next;
 5862        }
 63
 1264        if (cyclePosition == array.Length - 1)
 265        {
 266            cycleNode = lastNode;
 267        }
 68
 1269        if (cycleNode != null)
 1270        {
 1271            lastNode.next = cycleNode;
 1272        }
 73
 1274        return head;
 1875    }
 76}