| | | 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 | | using LeetCode.Core.Models; |
| | | 13 | | |
| | | 14 | | namespace LeetCode.Algorithms.DoubleNumberRepresentedAsLinkedList; |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public sealed class DoubleNumberRepresentedAsLinkedListIterative : IDoubleNumberRepresentedAsLinkedList |
| | | 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 ListNode? DoubleIt(ListNode? head) |
| | 2 | 26 | | { |
| | 2 | 27 | | ListNode? previous = null; |
| | | 28 | | |
| | 2 | 29 | | var current = head; |
| | | 30 | | |
| | 8 | 31 | | while (current != null) |
| | 6 | 32 | | { |
| | 6 | 33 | | var next = current.next; |
| | | 34 | | |
| | 6 | 35 | | current.next = previous; |
| | 6 | 36 | | previous = current; |
| | 6 | 37 | | current = next; |
| | 6 | 38 | | } |
| | | 39 | | |
| | 2 | 40 | | var reversedHead = previous; |
| | | 41 | | |
| | 2 | 42 | | var node = reversedHead; |
| | 2 | 43 | | var carry = 0; |
| | | 44 | | |
| | 2 | 45 | | ListNode? lastNode = null; |
| | | 46 | | |
| | 8 | 47 | | while (node != null) |
| | 6 | 48 | | { |
| | 6 | 49 | | var newVal = (node.val * 2) + carry; |
| | | 50 | | |
| | 6 | 51 | | if (newVal > 9) |
| | 5 | 52 | | { |
| | 5 | 53 | | carry = 1; |
| | | 54 | | |
| | 5 | 55 | | newVal %= 10; |
| | 5 | 56 | | } |
| | | 57 | | else |
| | 1 | 58 | | { |
| | 1 | 59 | | carry = 0; |
| | 1 | 60 | | } |
| | | 61 | | |
| | 6 | 62 | | node.val = newVal; |
| | 6 | 63 | | lastNode = node; |
| | 6 | 64 | | node = node.next; |
| | 6 | 65 | | } |
| | | 66 | | |
| | 2 | 67 | | if (lastNode != null && carry > 0) |
| | 1 | 68 | | { |
| | 1 | 69 | | lastNode.next = new ListNode(carry); |
| | 1 | 70 | | } |
| | | 71 | | |
| | 2 | 72 | | previous = null; |
| | 2 | 73 | | current = reversedHead; |
| | | 74 | | |
| | 9 | 75 | | while (current != null) |
| | 7 | 76 | | { |
| | 7 | 77 | | var next = current.next; |
| | 7 | 78 | | current.next = previous; |
| | 7 | 79 | | previous = current; |
| | 7 | 80 | | current = next; |
| | 7 | 81 | | } |
| | | 82 | | |
| | 2 | 83 | | return previous; |
| | 2 | 84 | | } |
| | | 85 | | } |