예제 #1
0
        public static LinkedNode <T> GetIntersectionNode <T>(LinkedNode <T> headA, LinkedNode <T> headB)
        {
            LinkedNode <T> A = headA, B = headB;

            while (A != B)
            {
                A = A != null ? A.Next : headB;
                B = B != null ? B.Next : headA;
            }
            return(A);
        }
예제 #2
0
 public void RemoveTargetNode(LinkedNode <T> target)
 {
     if (target.Next != null)
     {
         target.Value = target.Next.Value;
         target.Next  = target.Next.Next;
     }
     else
     {
         RemoveLast();
     }
 }
예제 #3
0
        private static int GetListLength <T>(LinkedNode <T> head)
        {
            int count = 0;

            while (head != null)
            {
                count++;
                head = head.Next;
            }

            return(count);
        }
예제 #4
0
        public void AddLast(T newValue)
        {
            var newNode = new LinkedNode <T>(newValue);

            if (Head == null)
            {
                Head = newNode;
            }
            else
            {
                Last().Next = newNode;
            }
        }
예제 #5
0
        public static LinkedNode <T> RecursiveReserve <T>(LinkedNode <T> node)
        {
            if (node == null || node.Next == null)
            {
                return(node);
            }

            var temp = RecursiveReserve <T>(node.Next);

            node.Next.Next = node;
            node.Next      = null;

            return(temp);
        }
예제 #6
0
        public void Reverse()
        {
            var            cur  = Head;
            LinkedNode <T> prev = null;

            while (cur != null)
            {
                var temp = cur.Next;
                cur.Next = prev;

                prev = cur;
                cur  = temp;
            }

            Head = prev;
        }
예제 #7
0
        public static LinkedNode <T> DeleteNode <T>(LinkedNode <T> head, int val)
        {
            if (head.Value.Equals(val))
            {
                return(head.Next);
            }

            var prev = head;

            while (prev.Next.Value.Equals(val))
            {
                prev = prev.Next;
            }
            prev.Next = prev.Next.Next;
            return(head);
        }
예제 #8
0
        public static LinkedNode <T> Reverse <T>(LinkedNode <T> head)
        {
            var            cur  = head;
            LinkedNode <T> prev = null;

            while (cur != null)
            {
                var temp = cur.Next;
                cur.Next = prev;

                prev = cur;
                cur  = temp;
            }

            return(prev);
        }
예제 #9
0
        public static T MiddleValue <T>(LinkedNode <T> head)
        {
            var p1 = head;
            var p2 = head;

            while (p2.Next != null)
            {
                p1 = p1.Next;
                p2 = p2.Next.Next;
                if (p1 == p2)
                {
                    throw new Exception("Cannot find middle value since linked list is circled");
                }
            }

            return(p1.Value);
        }
예제 #10
0
        public static bool IsCircled <T>(LinkedNode <T> head)
        {
            var p1 = head;
            var p2 = head;

            while (p2.Next != null)
            {
                p1 = p1.Next;
                p2 = p2.Next.Next;
                if (p1 == p2)
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #11
0
        public static LinkedNode <T> Append <T>(LinkedNode <T> head, T newValue)
        {
            var newNode = new LinkedNode <T>(newValue);

            if (head == null)
            {
                return(newNode);
            }

            var node = head;

            while (node.Next != null)
            {
                node = node.Next;
            }

            node.Next = newNode;
            return(head);
        }
예제 #12
0
        public LinkedNode Sum(LinkedNode head1, LinkedNode head2)
        {
            if (head1 == null && head2 == null)
            {
                throw new Exception("Both input linked lists are empty");
            }
            if (head1 == null || head2 == null)
            {
                return(head1 ?? head2);
            }

            var        preDigit = 0;
            LinkedNode res      = null;
            LinkedNode head     = null;

            while (head1 != null || head2 != null || preDigit != 0)
            {
                var list1Val = head1?.Value ?? 0;
                var list2Val = head2?.Value ?? 0;
                var sum      = list1Val + list2Val + preDigit;
                var digitRes = sum % 10;
                preDigit = sum / 10;

                if (res == null)
                {
                    res  = new LinkedNode(digitRes);
                    head = res;
                }
                else
                {
                    res.Next = new LinkedNode(digitRes);
                    res      = res.Next;
                }

                head1 = head1?.Next;
                head2 = head2?.Next;
            }

            return(head);
        }
예제 #13
0
        public static LinkedNode <T> DeleteNode2 <T>(LinkedNode <T> head, T val)
        {
            var            cur  = head;
            LinkedNode <T> prev = null;

            while (!cur.Value.Equals(val))
            {
                prev = cur;
                cur  = cur.Next;
            }

            if (prev == null)
            {
                head = head.Next;
            }
            else
            {
                prev.Next = cur.Next;
            }

            return(head);
        }
예제 #14
0
        public static LinkedNode <T> CommonNodeOfTwoLinkedList <T>(LinkedNode <T> headA, LinkedNode <T> headB)
        {
            var lengthA = GetListLength(headA);
            var lengthB = GetListLength(headB);
            var diff    = lengthA - lengthB;

            if (diff < 0)
            {
                var temp = headA;
                headA = headB;
                headB = temp;
                diff  = -diff;
            }

            while (diff > 0)
            {
                headA = headA.Next;
                diff--;
            }

            while (headA != headB)
            {
                headA = headA.Next;
                headB = headB.Next;
            }

            return(headA);
        }