コード例 #1
0
        public ListNode SwapPairs(ListNode head)
        {
            if (head == null || head.next == null) return head;

            ListNode newHead = head.next;
            var left = head;
            var right = head.next;
            ListNode current = null;

            while (left != null && right != null)
            {
                var origLeft = left;
                var nextPair = right.next;
                left = right;
                left.next = origLeft;
                origLeft.next = nextPair;

                if (current != null)
                {
                    current.next = right;
                }
                current = origLeft;

                if (nextPair == null)
                {
                    break;
                }

                left = nextPair;
                right = nextPair.next;
            }

            return newHead;
        }
コード例 #2
0
        public void ConfirmGetValueOfLargeNumber()
        {
            var list2_1 = new ListNode(1);
            var list2_2 = new ListNode(9);
            var list2_3 = new ListNode(9);
            var list2_4 = new ListNode(9);
            var list2_5 = new ListNode(9);
            var list2_6 = new ListNode(9);
            var list2_7 = new ListNode(9);
            var list2_8 = new ListNode(9);
            var list2_9 = new ListNode(9);
            var list2_10 = new ListNode(9);
            list2_1.next = list2_2;
            list2_2.next = list2_3;
            list2_3.next = list2_4;
            list2_4.next = list2_5;
            list2_5.next = list2_6;
            list2_6.next = list2_7;
            list2_7.next = list2_8;
            list2_8.next = list2_9;
            list2_9.next = list2_10;

            var value = GetValueOfList(list2_1);
            Assert.That(value, Is.EqualTo(9999999991));
        }
コード例 #3
0
        public void ConfirmGetValueOfZeroList()
        {
            var list = new ListNode(0);

            var value = GetValueOfList(list);
            value.Should().Be(0);
        }
コード例 #4
0
        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            var firstValue = GetValueOfList(l1);
            var secondValue = GetValueOfList(l2);

            var sum = firstValue + secondValue;

            return TurnValueIntoList(sum);
        }
コード例 #5
0
        public ListNode RemoveNthFromEnd(ListNode head, int n)
        {
            if (head == null) return null;
            if (head.next == null) return null;

            var queue = new Queue<ListNode>();

            var current = head;
            do
            {
                queue.Enqueue(current);
                current = current.next;
                while (queue.Count > n)
                {
                    queue.Dequeue();
                }
            } while (current != null && current.next != null);

            var removingFirst = n > queue.Count;
            ListNode nodeToLinkForward;
            if (removingFirst)
            {
                queue.Dequeue();
                if (queue.Count > 0)
                {
                    nodeToLinkForward = queue.Dequeue();
                }
                else
                {
                    nodeToLinkForward = current;
                }
            }
            else
            {
                nodeToLinkForward = queue.Dequeue();
                if (queue.Count > 0)
                {
                    queue.Dequeue();
                    if (queue.Count > 0)
                    {
                        nodeToLinkForward.next = queue.Dequeue();
                    }
                    else
                    {
                        nodeToLinkForward.next = current;
                    }
                }
                else
                {
                    nodeToLinkForward.next = null;
                }
            }

            return removingFirst ? nodeToLinkForward : head;
        }
コード例 #6
0
        public void RemoveLastNodeFromTwo()
        {
            var node1 = new ListNode(1);
            var node2 = new ListNode(2);
            node1.next = node2;

            var result = RemoveNthFromEnd(node1, 1);

            Assert.That(result.val, Is.EqualTo(1));
            Assert.That(result.next, Is.Null);
        }
コード例 #7
0
        public void MergeSingleItemListsWithL2HavingSmallerValue()
        {
            var list1Node1 = new ListNode(2);
            var list2Node1 = new ListNode(1);

            var result = MergeTwoLists(list1Node1, list2Node1);

            Assert.That(result.val, Is.EqualTo(1));
            Assert.That(result.next.val, Is.EqualTo(2));
            Assert.That(result.next.next, Is.Null);
        }
コード例 #8
0
        public void RemoveFirstNodeFromThree()
        {
            var node1 = new ListNode(1);
            var node2 = new ListNode(2);
            var node3 = new ListNode(3);
            node1.next = node2;
            node2.next = node3;

            var result = RemoveNthFromEnd(node1, 3);

            Assert.That(result.val, Is.EqualTo(2));
            Assert.That(result.next.val, Is.EqualTo(3));
            Assert.That(result.next.next, Is.Null);
        }
コード例 #9
0
        public void MergeShort1ToLong2()
        {
            var list1Node1 = new ListNode(1);
            var list2Node1 = new ListNode(2);
            var list2Node2 = new ListNode(3);
            var list2Node3 = new ListNode(4);
            list2Node1.next = list2Node2;
            list2Node2.next = list2Node3;

            var result = MergeTwoLists(list1Node1, list2Node1);

            Assert.That(result.val, Is.EqualTo(1));
            Assert.That(result.next.val, Is.EqualTo(2));
            Assert.That(result.next.next.val, Is.EqualTo(3));
            Assert.That(result.next.next.next.val, Is.EqualTo(4));
            Assert.That(result.next.next.next.next, Is.Null);
        }
コード例 #10
0
        public ListNode MergeKLists(ListNode[] lists)
        {
            if (lists == null || lists.Length == 0) return null;
            if (lists.Length == 1) return lists[0];

            ListNode head = null;
            ListNode current = null;

            var toManipulate = new List<ListNode>(lists.Where(l => l != null));
            while (toManipulate.Count > 0)
            {
                int lowestValue = int.MaxValue;
                int lowestIdx = 0;
                for (var idx = 0; idx < toManipulate.Count; ++idx)
                {
                    if (toManipulate[idx].val < lowestValue)
                    {
                        lowestValue = toManipulate[idx].val;
                        lowestIdx = idx;
                    }
                }

                if (head == null)
                {
                    head = toManipulate[lowestIdx];
                    current = head;
                }
                else
                {
                    current.next = toManipulate[lowestIdx];
                    current = current.next;
                }

                var next = toManipulate[lowestIdx].next;
                if (next == null)
                {
                    toManipulate.RemoveAt(lowestIdx);
                }
                else
                {
                    toManipulate[lowestIdx] = next;
                }
            }

            return head;
        }
コード例 #11
0
        public void RunExample()
        {
            // [-2,5]
            // [-9,-6,-3,-1,1,6]
            var list1Node1 = new ListNode(-2);
            var list1Node2 = new ListNode(5);
            list1Node1.next = list1Node2;

            var list2Node1 = new ListNode(-9);
            var list2Node2 = new ListNode(-6);
            var list2Node3 = new ListNode(-3);
            var list2Node4 = new ListNode(-1);
            var list2Node5 = new ListNode(1);
            var list2Node6 = new ListNode(6);
            list2Node1.next = list2Node2;
            list2Node2.next = list2Node3;
            list2Node3.next = list2Node4;
            list2Node4.next = list2Node5;
            list2Node5.next = list2Node6;

            var result = MergeTwoLists(list1Node1, list2Node1);

            Assert.That(result.val, Is.EqualTo(-9));
            Assert.That(result.next.val, Is.EqualTo(-6));
            Assert.That(result.next.next.val, Is.EqualTo(-3));
            Assert.That(result.next.next.next.val, Is.EqualTo(-2));
            Assert.That(result.next.next.next.next.val, Is.EqualTo(-1));
            Assert.That(result.next.next.next.next.next.val, Is.EqualTo(1));
            Assert.That(result.next.next.next.next.next.next.val, Is.EqualTo(5));
            Assert.That(result.next.next.next.next.next.next.next.val, Is.EqualTo(6));
        }
コード例 #12
0
        public ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            if (l1 == null)
            {
                return l2;
            }
            if (l2 == null)
            {
                return l1;
            }

            var head = l1;
            var left = l1;
            var right = l2;
            if (l1.val > l2.val)
            {
                head = l2;
                left = l2;
                right = l1;
            }

            while (left != null && right != null)
            {
                if (left.next != null)
                {
                    while (left.next != null && left.next.val <= right.val)
                    {
                        left = left.next;
                    }
                }
                else
                {
                    left.next = right;
                    break;
                }

                var leftNext = left.next;
                left.next = right;
                if (leftNext == null) break;

                left = right;
                right = leftNext;
            }

            return head;
        }
コード例 #13
0
 private static ListNode GenerateList(int[] values)
 {
     ListNode first = new ListNode(values[0]);
     ListNode previous = first;
     for(var idx = 1; idx < values.Length; ++idx)
     {
         var next = new ListNode(values[idx]);
         previous.next = next;
         previous = next;
     }
     return first;
 }
コード例 #14
0
        public ListNode ReverseKGroup(ListNode head, int k)
        {
            if (k < 2) return head;

            ListNode newHead = null;
            ListNode lastGroupEnd = null;
            ListNode current = head;
            ListNode startGroup = null;

            var stack = new Stack<ListNode>();

            do
            {
                if (stack.Count == k)
                {
                    var groupNode = stack.Pop();

                    if (lastGroupEnd == null)
                    {
                        newHead = groupNode;
                    }
                    else
                    {
                        lastGroupEnd.next = groupNode;
                    }

                    while (stack.Count > 0)
                    {
                        var temp = stack.Pop();
                        groupNode.next = temp;
                        groupNode = temp;
                    }
                    lastGroupEnd = groupNode;
                    startGroup = current;
                }

                stack.Push(current);

                if (current != null)
                {
                    current = current.next;
                }
            } while (current != null);

            if (stack.Count == k)
            {
                var groupNode = stack.Pop();
                if (newHead == null) newHead = groupNode;
                var startFinalGroup = groupNode;

                while (stack.Count > 0)
                {
                    var temp = stack.Pop();
                    groupNode.next = temp;
                    groupNode = temp;
                }
                groupNode.next = null;
                if (lastGroupEnd != null)
                {
                    lastGroupEnd.next = startFinalGroup;
                }
                return newHead;
            }
            else
            {
                if (lastGroupEnd != null)
                {
                    lastGroupEnd.next = startGroup;
                }
            }

            return newHead ?? head;
        }
コード例 #15
0
        public void SingleNodeGroupOf2()
        {
            var list = new ListNode(1);

            var result = ReverseKGroup(list, 2);

            Assert.That(result.val, Is.EqualTo(1));
            Assert.That(result.next, Is.Null);
        }
コード例 #16
0
        public void RunZeroValueTest()
        {
            var list1_1 = new ListNode(0);

            var list2_1 = new ListNode(0);

            var result = AddTwoNumbers(list1_1, list2_1);

            result.val.Should().Be(0);
        }
コード例 #17
0
        public ListNode TurnValueIntoList(long value)
        {
            int lastDigit = GetLastDigit(value);
            var listNode = new ListNode(lastDigit);
            var currentNode = listNode;

            while (value >= 0)
            {
                value /= 10;
                lastDigit = GetLastDigit(value);

                if (value == 0) break;

                var nextNode = new ListNode(lastDigit);
                currentNode.next = nextNode;
                currentNode = nextNode;
            }

            return listNode;
        }
コード例 #18
0
        public void RunTenBillionTest()
        {
            var list1_1 = new ListNode(9);

            var list2_1 = new ListNode(1);
            var list2_2 = new ListNode(9);
            var list2_3 = new ListNode(9);
            var list2_4 = new ListNode(9);
            var list2_5 = new ListNode(9);
            var list2_6 = new ListNode(9);
            var list2_7 = new ListNode(9);
            var list2_8 = new ListNode(9);
            var list2_9 = new ListNode(9);
            var list2_10 = new ListNode(9);
            list2_1.next = list2_2;
            list2_2.next = list2_3;
            list2_3.next = list2_4;
            list2_4.next = list2_5;
            list2_5.next = list2_6;
            list2_6.next = list2_7;
            list2_7.next = list2_8;
            list2_8.next = list2_9;
            list2_9.next = list2_10;

            var result = AddTwoNumbers(list1_1, list2_1);

            var value = GetValueOfList(result);

            Assert.That(value, Is.EqualTo(10000000000));
            result.val.Should().Be(0);
            result.next.val.Should().Be(0);
            result.next.next.val.Should().Be(0);
            result.next.next.next.val.Should().Be(0);
            result.next.next.next.next.val.Should().Be(0);
            result.next.next.next.next.next.val.Should().Be(0);
            result.next.next.next.next.next.next.val.Should().Be(0);
            result.next.next.next.next.next.next.next.val.Should().Be(0);
            result.next.next.next.next.next.next.next.next.val.Should().Be(0);
            result.next.next.next.next.next.next.next.next.next.val.Should().Be(0);
            result.next.next.next.next.next.next.next.next.next.next.val.Should().Be(1);
        }
コード例 #19
0
        public void RunTest()
        {
            var list1_1 = new ListNode(2);
            var list1_2 = new ListNode(4);
            var list1_3 = new ListNode(3);
            list1_1.next = list1_2;
            list1_2.next = list1_3;

            var list2_1 = new ListNode(5);
            var list2_2 = new ListNode(6);
            var list2_3 = new ListNode(4);
            list2_1.next = list2_2;
            list2_2.next = list2_3;

            var result = AddTwoNumbers(list1_1, list2_1);

            result.val.Should().Be(7);
            result.next.val.Should().Be(0);
            result.next.next.val.Should().Be(8);
        }
コード例 #20
0
        public long GetValueOfList(ListNode listNode)
        {
            long value = 0;
            long multiplier = 1;
            do
            {
                value += (listNode.val*multiplier);
                listNode = listNode.next;
                multiplier *= 10;
            } while (listNode != null && listNode.next != null);

            if (listNode != null)
                value += (listNode.val*multiplier);

            return value;
        }
コード例 #21
0
        public void ConfirmValueExtractor()
        {
            var list1_1 = new ListNode(2);
            var list1_2 = new ListNode(4);
            var list1_3 = new ListNode(3);
            list1_1.next = list1_2;
            list1_2.next = list1_3;

            var value = GetValueOfList(list1_1);
            value.Should().Be(342);

            var list2_1 = new ListNode(5);
            var list2_2 = new ListNode(6);
            var list2_3 = new ListNode(4);
            list2_1.next = list2_2;
            list2_2.next = list2_3;

            value = GetValueOfList(list2_1);
            value.Should().Be(465);
        }
コード例 #22
0
        public void RunTest()
        {
            var list1Node1 = new ListNode(1);
            var list1Node2 = new ListNode(2);
            list1Node1.next = list1Node2;

            var list2Node1 = new ListNode(2);
            var list2Node2 = new ListNode(3);
            list2Node1.next = list2Node2;

            var result = MergeTwoLists(list1Node1, list2Node1);

            Assert.That(result.val, Is.EqualTo(1));
            Assert.That(result.next.val, Is.EqualTo(2));
            Assert.That(result.next.next.val, Is.EqualTo(2));
            Assert.That(result.next.next.next.val, Is.EqualTo(3));
            Assert.That(result.next.next.next.next, Is.Null);
        }
コード例 #23
0
        public void RunExample()
        {
            var node1 = new ListNode(1);
            var node2 = new ListNode(2);
            var node3 = new ListNode(3);
            var node4 = new ListNode(4);
            var node5 = new ListNode(5);
            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            node4.next = node5;

            var result = RemoveNthFromEnd(node1, 2);

            Assert.That(result, Is.Not.Null, "Have head");
            Assert.That(result.val, Is.EqualTo(1), "Head 1");
            Assert.That(result.next, Is.Not.Null, "Head + 1");
            Assert.That(result.next.val, Is.EqualTo(2), "2");
            Assert.That(result.next.next.val, Is.EqualTo(3), "3");
            Assert.That(result.next.next.next.val, Is.EqualTo(5), "5");
            Assert.That(result.next.next.next.next, Is.Null, "End of list");
        }
コード例 #24
0
        public void SingleNodeList()
        {
            var node = new ListNode(1);

            var result = RemoveNthFromEnd(node, 1);

            Assert.That(result, Is.Null);
        }