| | | 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 | | |
| | | 14 | | using LeetCode.Core.Exceptions; |
| | | 15 | | |
| | | 16 | | namespace LeetCode.Core.Models; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Definition for singly-linked list |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class ListNode |
| | | 22 | | { |
| | | 23 | | public ListNode? next; |
| | | 24 | | |
| | | 25 | | public int val; |
| | | 26 | | |
| | 1309 | 27 | | public ListNode(int val = 0, ListNode? next = null) |
| | 1309 | 28 | | { |
| | 1309 | 29 | | this.next = next; |
| | 1309 | 30 | | this.val = val; |
| | 1309 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static ListNode ToListNodeOrThrow(int[] array) |
| | 33 | 34 | | { |
| | 33 | 35 | | return ToListNode(array) ?? throw new ListNodeBuildException(); |
| | 33 | 36 | | } |
| | | 37 | | |
| | | 38 | | public static ListNode? ToListNode(int[] array) |
| | 353 | 39 | | { |
| | 1516 | 40 | | return array.Reverse().Aggregate<int, ListNode?>(null, (next, val) => new ListNode(val, next)); |
| | 353 | 41 | | } |
| | | 42 | | |
| | | 43 | | public static ListNode? ToCycledListNode(int[] array, int cyclePosition) |
| | 18 | 44 | | { |
| | 18 | 45 | | var head = ToListNode(array); |
| | | 46 | | |
| | 18 | 47 | | if (head == null || cyclePosition < 0) |
| | 6 | 48 | | { |
| | 6 | 49 | | return head; |
| | | 50 | | } |
| | | 51 | | |
| | 24 | 52 | | ListNode? lastNode = head, cycleNode = null; |
| | | 53 | | |
| | 140 | 54 | | for (var index = 0; lastNode.next != null; index++) |
| | 58 | 55 | | { |
| | 58 | 56 | | if (index == cyclePosition) |
| | 10 | 57 | | { |
| | 10 | 58 | | cycleNode = lastNode; |
| | 10 | 59 | | } |
| | | 60 | | |
| | 58 | 61 | | lastNode = lastNode.next; |
| | 58 | 62 | | } |
| | | 63 | | |
| | 12 | 64 | | if (cyclePosition == array.Length - 1) |
| | 2 | 65 | | { |
| | 2 | 66 | | cycleNode = lastNode; |
| | 2 | 67 | | } |
| | | 68 | | |
| | 12 | 69 | | if (cycleNode != null) |
| | 12 | 70 | | { |
| | 12 | 71 | | lastNode.next = cycleNode; |
| | 12 | 72 | | } |
| | | 73 | | |
| | 12 | 74 | | return head; |
| | 18 | 75 | | } |
| | | 76 | | } |