< Summary

Information
Class: LeetCode.Algorithms.DesignStackWithIncrementOperation.DesignStackWithIncrementOperationLinkedList
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignStackWithIncrementOperation\DesignStackWithIncrementOperationLinkedList.cs
Line coverage
100%
Covered lines: 49
Uncovered lines: 0
Coverable lines: 49
Total lines: 112
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
.ctor(...)100%11100%
Push(...)100%44100%
Pop()100%44100%
Increment(...)100%44100%
.ctor(...)100%11100%
get_Value()100%11100%
get_Next()100%11100%
get_Previous()100%11100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\DesignStackWithIncrementOperation\DesignStackWithIncrementOperationLinkedList.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
 12namespace LeetCode.Algorithms.DesignStackWithIncrementOperation;
 13
 14/// <inheritdoc />
 15public class DesignStackWithIncrementOperationLinkedList : IDesignStackWithIncrementOperation
 16{
 17    private readonly int _maxSize;
 18    private int _count;
 19    private Node? _headNode;
 20    private Node? _tailNode;
 21
 122    public DesignStackWithIncrementOperationLinkedList(int maxSize)
 123    {
 124        _maxSize = maxSize;
 125    }
 26
 27    /// <summary>
 28    ///     Time complexity - O(1)
 29    ///     Space complexity - O(1)
 30    /// </summary>
 31    /// <param name="x"></param>
 32    public void Push(int x)
 533    {
 534        if (_count >= _maxSize)
 135        {
 136            return;
 37        }
 38
 439        var newNode = new Node(x, _headNode);
 40
 441        if (_headNode == null)
 142        {
 143            _tailNode = newNode;
 144        }
 45        else
 346        {
 347            _headNode.Previous = newNode;
 348        }
 49
 450        _headNode = newNode;
 51
 452        _count++;
 553    }
 54
 55    /// <summary>
 56    ///     Time complexity - O(1)
 57    ///     Space complexity - O(1)
 58    /// </summary>
 59    /// <returns></returns>
 60    public int Pop()
 561    {
 562        if (_headNode == null)
 163        {
 164            return -1;
 65        }
 66
 467        var value = _headNode.Value;
 68
 469        _headNode = _headNode.Next;
 70
 471        if (_headNode != null)
 372        {
 373            _headNode.Previous = null;
 374        }
 75        else
 176        {
 177            _tailNode = null;
 178        }
 79
 480        _count--;
 81
 482        return value;
 583    }
 84
 85    /// <summary>
 86    ///     Time complexity - O(min(k,n))
 87    ///     Space complexity - O(1)
 88    /// </summary>
 89    /// <param name="k"></param>
 90    /// <param name="val"></param>
 91    public void Increment(int k, int val)
 292    {
 293        var i = 0;
 294        var currentNode = _tailNode;
 95
 796        while (i < k && currentNode != null)
 597        {
 598            currentNode.Value += val;
 99
 5100            currentNode = currentNode.Previous;
 101
 5102            i++;
 5103        }
 2104    }
 105
 4106    private class Node(int value, Node? next = null, Node? previous = null)
 107    {
 18108        public int Value { get; set; } = value;
 8109        public Node? Next { get; } = next;
 15110        public Node? Previous { get; set; } = previous;
 111    }
 112}