< Summary

Information
Class: LeetCode.Algorithms.PalindromeLinkedList.PalindromeLinkedListSlowFastPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PalindromeLinkedList\PalindromeLinkedListSlowFastPointers.cs
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 74
Line coverage: 100%
Branch coverage
77%
Covered branches: 17
Total branches: 22
Branch coverage: 77.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
IsPalindrome(...)75%2020100%
ReverseList(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\PalindromeLinkedList\PalindromeLinkedListSlowFastPointers.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.PalindromeLinkedList;
 15
 16/// <inheritdoc />
 17public class PalindromeLinkedListSlowFastPointers : IPalindromeLinkedList
 18{
 19    /// <summary>
 20    ///     Time complexity - O(n)
 21    ///     Space complexity - O(1)
 22    /// </summary>
 23    /// <param name="head"></param>
 24    /// <returns></returns>
 25    public bool IsPalindrome(ListNode? head)
 1326    {
 1327        if (head?.next == null)
 128        {
 129            return true;
 30        }
 31
 1232        var slow = head;
 1233        var fast = head;
 34
 2735        while (fast.next is { next: not null })
 1536        {
 1537            slow = slow?.next;
 1538            fast = fast.next.next;
 1539        }
 40
 41
 1242        var secondHalfStart = ReverseList(slow?.next);
 43
 1244        var firstHalfStart = head;
 45
 2846        while (secondHalfStart != null)
 2047        {
 2048            if (firstHalfStart != null && firstHalfStart.val != secondHalfStart.val)
 449            {
 450                return false;
 51            }
 52
 1653            firstHalfStart = firstHalfStart?.next;
 1654            secondHalfStart = secondHalfStart.next;
 1655        }
 56
 857        return true;
 1358    }
 59
 60    private static ListNode? ReverseList(ListNode? head)
 1261    {
 1262        ListNode? reverseHead = null;
 63
 3564        while (head != null)
 2365        {
 2366            var nextTemp = head.next;
 2367            head.next = reverseHead;
 2368            reverseHead = head;
 2369            head = nextTemp;
 2370        }
 71
 1272        return reverseHead;
 1273    }
 74}