Exemple #1
0
        public static ListNode Process_Recursively(ListNode first, ListNode second)
        {
            if (first == null)
            {
                return second;
            }

            if (second == null)
            {
                return first;
            }

            ListNode newHead = null;
            if (first.Value <= second.Value)
            {
                newHead = first;
                newHead.Next = Process_Recursively(first.Next, second);
            }
            else
            {
                newHead = second;
                newHead.Next = Process_Recursively(first, second.Next);
            }

            return newHead;
        }
Exemple #2
0
        public void TestAddToTailWithOneNode()
        {
            ListNode head = new ListNode(1);
            ListNode.AddToTail(ref head, 5);

            Assert.IsTrue(Helper.CompareLinkedList(Helper.CreateLinkedList(new int[] { 1, 5 }), head));
        }
Exemple #3
0
        public static bool CompareLinkedList(ListNode first, ListNode second)
        {
            if (first == second)
            {
                return true;
            }

            if (first == null || second == null)
            {
                return false;
            }

            bool result = true;
            while (first != null && second != null)
            {
                if (!first.Equals(second))
                {
                    result = false;
                    break;
                }

                first = first.Next;
                second = second.Next;
            }

            return result && first == null && second == null;
        }
Exemple #4
0
        public void TestOnlyOneNode()
        {
            ListNode head = new ListNode(1);
            Assert.AreEqual(new ListNode(1), ReverseList.Process(head));

            head = new ListNode(1);
            Assert.AreEqual(new ListNode(1), ReverseList.Process_Recursively(head));
        }
        public static void ProcessRecursively(ListNode head)
        {
            if (head == null)
            {
                return;
            }

            ProcessRecursively(head.Next);
            output.Append(head.Value);
        }
        public void TestOnlyOneNode()
        {
            ListNode head = new ListNode(4);
            string expected = "4";

            PrintListInReversedOrder.ProcessRecursively(head);
            Assert.AreEqual(expected, PrintListInReversedOrder.Result);

            PrintListInReversedOrder.ProcessIteratively(head);
            Assert.AreEqual(expected, PrintListInReversedOrder.Result);
        }
        public static void ProcessIteratively(ListNode head)
        {
            Stack<ListNode> stack = new Stack<ListNode>();
            while (head != null)
            {
                stack.Push(head);
                head = head.Next;
            }

            while (stack.Count > 0)
            {
                output.Append(stack.Pop().Value);
            }
        }
Exemple #8
0
        public static ListNode Process(ListNode first, ListNode second)
        {
            if (first == null)
            {
                return second;
            }

            if (second == null)
            {
                return first;
            }

            ListNode newHead = new ListNode(-1);

            ListNode previous = newHead;
            while (first != null && second != null)
            {
                if (first.Value <= second.Value)
                {
                    previous.Next = first;
                    first = first.Next;
                }
                else
                {
                    previous.Next = second;
                    second = second.Next;
                }

                previous = previous.Next;
            }

            if (first != null)
            {
                previous.Next = first;
            }

            if (second != null)
            {
                previous.Next = second;
            }

            return newHead.Next;
        }
        public ListNode ReverseList(ListNode head)
        {
            if (head == null)
            {
                return(null);
            }
            else if (head.next == null)
            {
                return(head);
            }
            else
            {
                ListNode tail    = head.next;
                ListNode newHead = ReverseList(head.next);
                head.next = null;
                tail.next = head;

                return(newHead);
            }
        }
        public void TestOnlyOneNode()
        {
            ListNode first = new ListNode(2);
            ListNode second = new ListNode(1);

            Assert.IsTrue(Helper.CompareLinkedList(
                Helper.CreateLinkedList(new int[] { 1, 2 }),
                MergeSortedList.Process(first, second)));

            first = new ListNode(2);
            second = new ListNode(1);

            Assert.IsTrue(Helper.CompareLinkedList(
                Helper.CreateLinkedList(new int[] { 1, 2 }),
                MergeSortedList.Process_Recursively(first, second)));
        }
 public void TestOnlyOneNode()
 {
     ListNode head = new ListNode(1);
     Assert.AreEqual(1, FindMiddleNodeOfList.Process(head).Value);
 }
Exemple #12
0
        public void TestRemoveNodeWithOnlyNode()
        {
            ListNode head = new ListNode(5);
            ListNode.RemoveNode(ref head, 5);

            Assert.IsNull(head);
        }
 public void TestOnlyOneNode()
 {
     ListNode head = new ListNode(1);
     Assert.IsFalse(FindLoopInList.Process(head));
 }
 public void TestOnlyOneNode()
 {
     ListNode head = new ListNode(1);
     Assert.AreEqual(1, KthNodeFromEnd.Process(head, 1).Value);
 }
Exemple #15
0
        public static ListNode CreateLinkedList(int[] values)
        {
            if (values == null || values.Length == 0)
            {
                return null;
            }

            ListNode head = new ListNode(values[0]);
            ListNode tail = head;

            for (int i = 1; i < values.Length; ++i)
            {
                tail.Next = new ListNode(values[i]);
                tail = tail.Next;
            }

            return head;
        }
Exemple #16
0
        public static ListNode GetLastNode(ListNode head)
        {
            if (head == null)
            {
                return null;
            }

            ListNode lastNode = head;
            while (lastNode.Next != null)
            {
                lastNode = lastNode.Next;
            }

            return lastNode;
        }