Exemplo n.º 1
0
        public LinkedListNode FindStartOfCycle(LinkedListNode head)
        {
            LinkedListNode fast = head;
            LinkedListNode slow = head;

            while (fast!=null && slow!=null && fast.Next!=null) {
                slow = slow.Next;
                fast = fast.Next.Next;

                if (slow==fast) {
                    int cycleLen = 0;

                    do {
                        cycleLen++;
                        slow = slow.Next;
                    } while (fast!=slow);

                    slow = head;
                    fast = head;

                    for (int i = 0; i < cycleLen; i++) {
                        fast = fast.Next;
                    }

                    while (slow!=fast) {
                        slow = slow.Next;
                        fast = fast.Next;
                    }

                    return slow;
                }
            }

            return null;
        }
Exemplo n.º 2
0
        private LinkedListNode Add(LinkedListNode l1, LinkedListNode l2, int carry)
        {
            if (l1 == null && l2 == null)
            {
                return null;
            }

            int value = carry;
            if (l1 != null)
            {
                value += l1.Value;
            }
            if (l2 != null)
            {
                value += l2.Value;
            }

            var more = Add(l1 == null ? null : l1.Next,
                           l2 == null ? null : l2.Next,
                            value > 9 ? 1 : 0);

            var result = new LinkedListNode(value % 10);
            result.Next = more;
            return result;
        }
Exemplo n.º 3
0
        public LinkedListNode FindStartingNodeOfOverlappingPart(LinkedListNode h1, LinkedListNode h2)
        {
            // Get the length for both lists
            int len1 = LinkedListNode.GetLength(h1);
            int len2 = LinkedListNode.GetLength(h2);

            // Advance the longer list
            LinkedListNode longList;
            LinkedListNode shortList;
            if (len1 > len2) {
                longList = h1;
                shortList = h2;
            } else {
                longList = h2;
                shortList = h1;
            }
            for (int i = 0; i < Math.Abs(len1-len2); i++) {
                longList = longList.Next;
            }

            // Advance both lists and compare node
            while (longList!=null && shortList!=null && longList!=shortList) {
                longList = longList.Next;
                shortList = shortList.Next;
            }

            return longList;
        }
Exemplo n.º 4
0
        public LinkedListNode ReverseLinkedListInPairs(LinkedListNode head)
        {
            if (head == null)
            {
                return null;
            }

            var first = head;
            var second = first.Next;

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

            while (first != null && second != null)
            {
                var temp = first.Next.Next;
                second.Next = first;
                first.Next = temp;
                first = temp;

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

            return head.Next;
        }
Exemplo n.º 5
0
        public LinkedListNode FindTheNthToLastElement(LinkedListNode head, int n)
        {
            if (head == null || n < 1)
            {
                return null;
            }

            var front = head;
            var back = head;

            for (int i = 0; i < n; i++)
            {
                if (front == null)
                {
                    return null;
                }

                front = front.Next;
            }

            while (front.Next != null)
            {
                front = front.Next;
                back = back.Next;
            }

            return back;
        }
Exemplo n.º 6
0
        public LinkedListNode Merge(LinkedListNode a, LinkedListNode b)
        {
            var head = new LinkedListNode(100);
            var current = head;
            while (a != null && b != null) {
                if (a.Value < b.Value) {
                    current.Next = a;
                    a = a.Next;
                } else {
                    current.Next = b;
                    b = b.Next;
                }
                current = current.Next;
            }

            if (a != null) {
                current.Next = a;
            }

            if (b != null) {
                current.Next = b;
            }

            return head.Next;
        }
 public void BuildLinkedList()
 {
     node = new LinkedListNode(17);
     node.Next = new LinkedListNode(2);
     node.Next.Next = new LinkedListNode(2);
     node.Next.Next.Next = new LinkedListNode(3);
     node.Next.Next.Next.Next = new LinkedListNode(6);
     node.Next.Next.Next.Next.Next = node;
 }
Exemplo n.º 8
0
        public void It_should_return_null_if_there_is_no_cycle()
        {
            var head = new LinkedListNode(2);
            head.Next = new LinkedListNode(5);
            head.Next.Next = new LinkedListNode(7);
            head.Next.Next.Next = new LinkedListNode(3);
            head.Next.Next.Next.Next = new LinkedListNode(11);

            Obj.FindStartOfCycle(head).Should().BeNull();
        }
Exemplo n.º 9
0
        public void Build_linked_lists()
        {
            h1 = new LinkedListNode(3);
            h1.Next = new LinkedListNode(1);
            h1.Next.Next = new LinkedListNode(5);

            h2 = new LinkedListNode(5);
            h2.Next = new LinkedListNode(9);
            h2.Next.Next = new LinkedListNode(2);
        }
Exemplo n.º 10
0
 public void Build_linked_list()
 {
     head = new LinkedListNode(2);
     head.Next = new LinkedListNode(3);
     head.Next.Next = new LinkedListNode(5);
     head.Next.Next.Next = new LinkedListNode(7);
     head.Next.Next.Next.Next = new LinkedListNode(3);
     head.Next.Next.Next.Next.Next = new LinkedListNode(11);
     head.Next.Next.Next.Next.Next.Next = new LinkedListNode(3);
     head.Next.Next.Next.Next.Next.Next.Next = new LinkedListNode(5);
 }
Exemplo n.º 11
0
        public void Build_linked_lists()
        {
            h1 = new LinkedListNode(6);
            h1.Next = new LinkedListNode(17);
            h1.Next.Next = new LinkedListNode(2);
            h1.Next.Next.Next = new LinkedListNode(2);
            h1.Next.Next.Next.Next = new LinkedListNode(3);

            h2 = new LinkedListNode(2);
            h2.Next = new LinkedListNode(3);
            h2.Next.Next = new LinkedListNode(5);
            h2.Next.Next.Next = new LinkedListNode(4);
            h2.Next.Next.Next.Next = new LinkedListNode(2);
            h2.Next.Next.Next.Next.Next = h1.Next.Next;
        }
Exemplo n.º 12
0
        public void It_should_return_start_of_the_cycle_if_there_is_a_cycle()
        {
            var head = new LinkedListNode(2);
            head.Next = new LinkedListNode(5);
            head.Next.Next = new LinkedListNode(7);
            head.Next.Next.Next = new LinkedListNode(3);
            head.Next.Next.Next.Next = new LinkedListNode(11);
            head.Next.Next.Next.Next.Next = new LinkedListNode(6);
            head.Next.Next.Next.Next.Next.Next = new LinkedListNode(7);
            head.Next.Next.Next.Next.Next.Next.Next = new LinkedListNode(2);
            head.Next.Next.Next.Next.Next.Next.Next.Next = new LinkedListNode(2);
            head.Next.Next.Next.Next.Next.Next.Next.Next.Next = new LinkedListNode(3);
            head.Next.Next.Next.Next.Next.Next.Next.Next.Next.Next = head.Next.Next.Next.Next.Next;

            Obj.FindStartOfCycle(head).Should().Be(head.Next.Next.Next.Next.Next);
        }
Exemplo n.º 13
0
        public LinkedListNode RemoveDuplicate(LinkedListNode head)
        {
            LinkedListNode curr = head;
            LinkedListNode pre = null;
            var buffer = new Dictionary<int, bool>();

            while (curr != null) {
                if (buffer.ContainsKey(curr.Value)) {
                    pre.Next = curr.Next;
                } else {
                    buffer.Add(curr.Value, true);
                    pre = curr;
                }

                curr = curr.Next;
            }

            return head;
        }
Exemplo n.º 14
0
        public void It_should_reverse_linked_list_iteratively()
        {
            Obj.ReverseLinkedListIterative(ref head);
            head.Value.Should().Be(5);
            head.Next.Value.Should().Be(3);
            head.Next.Next.Value.Should().Be(11);
            head.Next.Next.Next.Value.Should().Be(3);
            head.Next.Next.Next.Next.Value.Should().Be(7);
            head.Next.Next.Next.Next.Next.Value.Should().Be(5);
            head.Next.Next.Next.Next.Next.Next.Value.Should().Be(3);
            head.Next.Next.Next.Next.Next.Next.Next.Value.Should().Be(2);

            head = new LinkedListNode(10);
            Obj.ReverseLinkedListIterative(ref head);
            head.Value.Should().Be(10);
            head.Next.Should().BeNull();

            head = null;
            Obj.ReverseLinkedListIterative(ref head);
            head.Should().BeNull();
        }
        public double GetMedian(LinkedListNode node)
        {
            LinkedListNode curr = node;
            LinkedListNode start = node;
            int count = 0;

            // get total number of nodes and the node with minimum value
            do {
                count++;
                curr = curr.Next;
                if (start.Value <= node.Value) {
                    start = curr;
                }
            } while (curr != node);
            start = start.Next;

            // go to the middle of the list
            for (int i = 0; i < (count - 1) / 2; i++) {
                start = start.Next;
            }

            return count % 2 == 1 ? start.Value : (start.Value + start.Next.Value) / 2.0;
        }
Exemplo n.º 16
0
 public LinkedListNode Add(LinkedListNode l1, LinkedListNode l2)
 {
     return Add(l1, l2, 0);
 }
Exemplo n.º 17
0
        public void ReverseLinkedListIterative(ref LinkedListNode head)
        {
            if (head == null) {
                return;
            }

            LinkedListNode pre = null;
            LinkedListNode curr = head;

            while (curr != null) {
                LinkedListNode next = curr.Next;
                curr.Next = pre;
                pre = curr;
                curr = next;
            }

            head = pre;
        }
Exemplo n.º 18
0
        public void ReverseLinkedListRecursive(ref LinkedListNode head)
        {
            if (head == null) {
                return;
            }

            var rest = head.Next;

            if (rest == null) {
                return;
            }

            ReverseLinkedListRecursive(ref rest);
            head.Next.Next = head;
            head.Next = null;
            head = rest;
        }