예제 #1
0
        //2->10-2->2
        public static ListNode removeAllMatchingElements(ListNode head, int val)
        {
            ListNode current  = head;
            ListNode Head     = head;
            ListNode previous = null;

            while (current != null)
            {
                if (current.val.Equals(val))
                {
                    var next = current.next;
                    if (current.Equals(Head))
                    {
                        if (next != null)
                        {
                            Head.val  = next.val;
                            Head.next = next.next;
                        }
                        else
                        {
                            Head = null;
                        }
                        current  = Head;
                        previous = null;
                    }
                    else
                    {
                        if (next != null)
                        {
                            current.val  = next.val;
                            current.next = next.next;
                        }
                        else
                        {
                            previous.next = null;
                            current       = null;
                        }
                    }
                }
                else
                {
                    previous = current;
                    current  = current.next;
                }
            }
            return(Head);
        }
예제 #2
0
        public static ListNode FindNodeIntersection(ListNode headA, ListNode headB)
        {
            ListNode node = null;

            int lenA     = 0;
            int lenB     = 0;
            var currentA = headA;
            var currentB = headB;

            while (currentA != null)
            {
                currentA = currentA.next;
                lenA++;
            }

            while (currentB != null)
            {
                currentB = currentB.next;
                lenB++;
            }

            if (lenA > lenB)
            {
                if (lenB > 1)
                {
                    var diff = lenA - lenB;
                    while (diff != 0)
                    {
                        headA = headA.next;
                        diff--;
                    }
                }
            }
            if (lenB > lenA)
            {
                if (lenA > 1)
                {
                    var diff = lenB = lenA;
                    while (diff != 0)
                    {
                        headB = headB.next;
                        diff--;
                    }
                }
            }

            while (headA != null && headB != null)
            {
                if (headA.Equals(headB))
                {
                    node = headA;
                    break;
                }
                if (lenA > 1)
                {
                    headA = headA.next;
                }
                if (lenB > 1)
                {
                    headB = headB.next;
                }
            }

            return(node);
        }
예제 #3
0
        public static ListNode RotateLinkedList(ListNode head, int k)
        {
            ListNode slowPtr  = head;
            ListNode fastPtr  = head;
            ListNode last     = null;
            ListNode previous = null;

            if (head == null)
            {
                return(null);
            }
            if (k == 0)
            {
                return(head);
            }
            int i = 0;

            while (true)
            {
                if (i < k)
                {
                    last    = fastPtr;
                    fastPtr = fastPtr.next;
                    i++;
                }
                if (i == k)
                {
                    break;
                }
                if (fastPtr == null)
                {
                    break;
                }
            }

            if (i < k)
            {
                k       = k % i;
                i       = 0;
                fastPtr = head;
            }

            if (k == 0)
            {
                return(head);
            }
            while (fastPtr != null)
            {
                if (i < k)
                {
                    fastPtr = fastPtr.next;
                    i++;
                }
                else
                {
                    last     = fastPtr;
                    previous = slowPtr;
                    fastPtr  = fastPtr.next;
                    slowPtr  = slowPtr.next;
                }
            }

            if (slowPtr != head)
            {
                if (slowPtr.Equals(last))
                {
                    previous.next = null;
                    var next = head.next;
                    slowPtr.next = head;
                    head         = slowPtr;
                }
                else
                {
                    previous.next = null;
                    last.next     = head;
                    head          = slowPtr;
                }
            }
            return(head);
        }