< Summary

Information
Class: LeetCode.Algorithms.ReorderList.ReorderListTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReorderList\ReorderListTwoPointers.cs
Line coverage
93%
Covered lines: 30
Uncovered lines: 2
Coverable lines: 32
Total lines: 70
Line coverage: 93.7%
Branch coverage
83%
Covered branches: 15
Total branches: 18
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReorderList(...)83.33%181893.75%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ReorderList\ReorderListTwoPointers.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.ReorderList;
 15
 16/// <inheritdoc />
 17public class ReorderListTwoPointers : IReorderList
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(n)
 22    /// </summary>
 23    /// <param name="head"></param>
 24    public void ReorderList(ListNode? head)
 225    {
 226        if (head?.next == null)
 027        {
 028            return;
 29        }
 30
 231        var slow = head;
 232        var fast = head;
 33
 534        while (fast.next is { next: not null })
 335        {
 336            slow = slow?.next;
 337            fast = fast.next.next;
 338        }
 39
 240        var stack = new Stack<ListNode>();
 41
 242        if (slow != null)
 243        {
 244            var current = slow.next;
 45
 646            while (current != null)
 447            {
 448                stack.Push(current);
 49
 450                current = current.next;
 451            }
 52
 253            slow.next = null;
 254        }
 55
 256        var left = head;
 57
 658        while (left != null && stack.Count > 0)
 459        {
 460            var current = stack.Pop();
 61
 462            var temp = left.next;
 63
 464            left.next = current;
 465            current.next = temp;
 66
 467            left = temp;
 468        }
 269    }
 70}