コード例 #1
0
ファイル: Sort List.cs プロジェクト: zcyGit/My-Algor
        private ListNode mergeSort(ListNode head, ListNode tail)
        {
            if (head.next == tail)
            { //single node
                head.next = null;
                return head;
            }

            ListNode itr = head, mid = head;
            while (itr != tail)
            {
                itr = itr.next;

                if (itr != tail)
                {
                    itr = itr.next;
                }

                mid = mid.next;
            }

            ListNode l1 = mergeSort(head, mid);

            ListNode l2 = mergeSort(mid, tail);

            return MergeListNode(l1, l2);
        }
コード例 #2
0
ファイル: Reorder List.cs プロジェクト: zcyGit/My-Algor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="head"></param>
        public static void ReorderList(ListNode head)
        {
            if (head == null || head.next == null) return;

            var reMidNode = FindMid(head);
            reMidNode = ReverseList(reMidNode);

            var beginNode = head;
            var tempNode = head;
            while (reMidNode != null)
            {
                tempNode = tempNode.next;

                beginNode.next = reMidNode;

                reMidNode = reMidNode.next;

                beginNode = beginNode.next;

                beginNode.next = tempNode;

                beginNode = beginNode.next;
            }
            beginNode.next = null;
        }
コード例 #3
0
        public static ListNode DeleteDuplicates(ListNode head)
        {
            if (head == null || head.next == null)
            {
                return head;
            }

            ListNode Cur = head;
            ListNode Perv = head.next;

            while (Perv != null)
            {

                if (Perv.val == Cur.val)
                {
                    Cur.next = Perv.next;
                    Perv = Perv.next;
                }
                else
                {
                    Cur = Cur.next;
                    Perv = Perv.next;
                }

            }

            return head;
        }
コード例 #4
0
        public static ListNode ReverseList(ListNode head, int m, int n)
        {
            if (head == null || m == n)
            {
                return head;
            }

            ListNode tempHead = new ListNode(0);
            tempHead.next = head;
            var mNode = tempHead;
            ListNode current, pnext;
            int i = 1;

            while (i < m)
            {
                mNode = mNode.next;
                i++;
            }

            current = mNode.next;

            while (current.next != null && i < n)
            {
                pnext = current.next;
                current.next = pnext.next;
                pnext.next = mNode.next;
                mNode.next = pnext;
                i++;
            }

            return tempHead.next;
        }
コード例 #5
0
ファイル: Sort List.cs プロジェクト: zcyGit/My-Algor
        public ListNode SortList(ListNode head)
        {
            if (head == null) //trap
                return null;

            return mergeSort(head, null);
        }
コード例 #6
0
        public ListNode SwapPairs(ListNode head)
        {
            ListNode tempHead = new ListNode(-1);
            tempHead.next = head;
            var node = tempHead;
            ListNode current, pnext;

            current = node.next;

            while (current != null)
            {
                pnext = current.next;

                if (pnext != null)
                {
                    //交换
                    current.next = pnext.next;
                    pnext.next = node.next;
                    node.next = pnext;
                    //重置
                    node = current;
                    current = node.next;

                }
                else
                {
                    break;
                }
            }

            return tempHead.next;
        }
コード例 #7
0
        /// <summary>
        /// 反转一个链表
        /// </summary>
        public static void Test()
        {
            ListNode l1 = new ListNode(2);
            ListNode l2 = new ListNode(3);
            ListNode l3 = new ListNode(4);

            l2.next = l3;
            l1.next = l2;

            var node = ReverseList(l1);
        }
コード例 #8
0
ファイル: Sort List.cs プロジェクト: zcyGit/My-Algor
        /// <summary>
        /// 时间复杂度要求要nlogn ,空间要求不开辟新的内存空间,
        /// 选择用归并排序来做
        /// </summary>
        public static void Test()
        {
            ListNode l1 = new ListNode(2);
            ListNode l2 = new ListNode(3);
            ListNode l3 = new ListNode(1);
            ListNode l4 = new ListNode(4);

            l2.next = l3;
            l1.next = l2;

            var list = new Sort_List().SortList(l1);
        }
コード例 #9
0
        public static void Test()
        {
            ListNode l1 = new ListNode(1);
            ListNode l2 = new ListNode(2);
            ListNode l3 = new ListNode(10);
            ListNode l4 = new ListNode(2);
            ListNode l5 = new ListNode(1);

            //l4.next = l5;
            //l3.next = l4;
            //l2.next = l3;
            l1.next = l2;

               Console.Write(IsPalindrome(l1));
        }
コード例 #10
0
 /// <summary>
 /// 找到中间列表
 /// </summary>
 /// <param name="head"></param>
 /// <returns></returns>
 protected static ListNode FindMid(ListNode head)
 {
     var beginNode = head;
     var midNode = head;
     while (beginNode != null)
     {
         beginNode = beginNode.next;
         midNode = midNode.next;
         if (beginNode != null)
         {
             beginNode = beginNode.next;
         }
     }
     return midNode;
 }
コード例 #11
0
        /// <summary>
        /// </summary>
        public static void Test()
        {
            ListNode l1 = new ListNode(2);
            ListNode l2 = new ListNode(3);
            ListNode l3 = new ListNode(4);

            l2.next = l3;
            l1.next = l2;

            var node = RemoveNthFromEnd(l1, 4);

            while (node != null)
            {
                Console.WriteLine(node.val);
                node = node.next;
            }
        }
コード例 #12
0
ファイル: Reorder List.cs プロジェクト: zcyGit/My-Algor
        public static void Test()
        {
            ListNode head = new ListNode(1);

            var tempHead = head;
            for (int i = 2; i < 4; i++)
            {
                ListNode temp = new ListNode(i);
                tempHead.next = temp;
                tempHead = tempHead.next;
            }
            ReorderList(head);
            while (head != null)
            {
                Console.WriteLine(head.val);
                head = head.next;
            }
        }
コード例 #13
0
        public static ListNode RemoveNthFromEnd(ListNode head, int n)
        {
            if (head == null)
            {
                return null;
            }

            if (n == 0)
            {
                return head;
            }

            ListNode tempHead = new ListNode(0);
            tempHead.next = head;

            ListNode current;
            current = tempHead.next;
            int totel = 0;

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

            int number = totel - n;
            current = tempHead;

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

            }

            return tempHead.next;
        }
コード例 #14
0
        public static void Test()
        {
            ListNode x = new ListNode(1);
            ListNode x1 = new ListNode(1);
            ListNode x2 = new ListNode(2);
            ListNode x3 = new ListNode(3);

            x2.next = x3;
            x1.next = x2;
            x.next = x1;

            var head = DeleteDuplicates(x);

            while (head != null)
            {
                Console.WriteLine(head.val);
                head = head.next;
            }
        }
コード例 #15
0
ファイル: OddEvenList.cs プロジェクト: zcyGit/My-Algor
        public static void Test()
        {
            ListNode head = new ListNode(1);

            var tempHead = head;
            for (int i = 2; i < 9; i++)
            {
                ListNode temp = new ListNode(i);
                tempHead.next = temp;
                tempHead = tempHead.next;
            }

            var list = OddEvenList(head);
            while (list != null)
            {
                Console.WriteLine(list.val);
                list = list.next;
            }
        }
コード例 #16
0
        public static ListNode MergeKLists(ListNode[] lists)
        {
            if (lists == null || lists.Count() == 0)
                return null;
            if (lists.Count() == 1)
            {
                return lists[0];
            }
            List<int> numbers = new List<int>();
            foreach (var list in lists)
            {
                var tempList = list;
                int middle = numbers.Count();
                while (tempList != null)
                {
                    numbers.Add(tempList.val);
                    tempList = tempList.next;
                }
                if (numbers.Count() > 0 && middle != numbers.Count())
                {
                    //两两归并
                    Merge(numbers, 0, middle, numbers.Count());
                }

            }
            if (numbers.Count() > 0)
            {
                ListNode node = new ListNode(numbers[0]);
                ListNode head = node;
                int i = 1;

                while (i < numbers.Count)
                {
                    node.next = new ListNode(numbers[i]);
                    node = node.next;
                    i++;
                }
                return head;
            }
            else
                return null;
        }
コード例 #17
0
        public static ListNode DeleteDuplicates(ListNode head)
        {
            if (head == null || head.next == null)
            {
                return head;
            }
            ListNode temp = new ListNode(int.MinValue);
            temp.next = head;

            ListNode Cur = temp;
            ListNode Next = temp.next;
            ListNode Pre = temp;

            while (Next != null)
            {
                if (Next.val == Cur.val)
                {
                    Next = Next.next;

                    while (Next != null && Next.val == Cur.val)
                    {
                        Next = Next.next;
                    }

                    Pre.next = Next;
                    Cur = Pre.next;
                    if (Next == null)
                    {
                        return temp.next;
                    }
                    Next = Next.next;
                }
                else
                {
                    Pre = Cur;
                    Cur = Cur.next;
                    Next = Next.next;
                }
            }

            return temp.next;
        }
コード例 #18
0
        public static void Test()
        {
            ListNode l1 = new ListNode(1);
            ListNode l2 = new ListNode(2);
            ListNode l3 = new ListNode(3);
            ListNode l4 = new ListNode(4);

            l3.next = l4;
            l2.next = l3;
            l1.next = l2;

            DeleteNode(l1);

            var temp = l1;
            while (temp != null)
            {
                Console.WriteLine(temp.val);
                temp = temp.next;
            }
        }
コード例 #19
0
        /// <summary>
        /// 反转一个链表
        /// </summary>
        public static void Test()
        {
            ListNode l1 = new ListNode(2);
            ListNode l2 = new ListNode(3);
            ListNode l3 = new ListNode(4);
            ListNode l4 = new ListNode(5);

            l3.next = l4;
            l2.next = l3;
            l1.next = l2;

            var node = ReverseList(l1, 2, 3);

            while (node != null)
            {
                Console.WriteLine(node.val);
                node = node.next;

            }
        }
コード例 #20
0
        public static void Test()
        {
            ListNode l1 = new ListNode(1);
            ListNode l2 = new ListNode(2);
            ListNode l3 = new ListNode(3);
            ListNode l4 = new ListNode(4);
            ListNode l5 = new ListNode(5);

            l4.next = l5;
            l3.next = l4;
            l2.next = l3;
            l1.next = l2;

            var node = new Swap_Nodes_in_Pairs().SwapPairs(l1);

            while (node != null)
            {
                Console.WriteLine(node.val);
                node = node.next;
            }
        }
コード例 #21
0
ファイル: OddEvenList.cs プロジェクト: zcyGit/My-Algor
        public static ListNode OddEvenList(ListNode head)
        {
            if (head == null || head.next == null || head.next.next == null) return head;

            var odd = head;
            var oddHead = odd;
            var even = head.next;
            var evenHead = even;

            while (even != null && even.next != null)
            {
                //奇数的下一个连接偶数的下一个
                odd.next = even.next;
                odd = odd.next;

                even.next = odd.next;
                even = even.next;
            }
            odd.next = evenHead;
            return oddHead;
        }
コード例 #22
0
        public static ListNode ReverseList(ListNode head)
        {
            if(head==null)
            {
                return null;
            }

            ListNode tempHead = new ListNode(0);
            tempHead.next = head;

            ListNode current, pnext;
            current = tempHead.next;

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

            return tempHead.next;
        }
コード例 #23
0
ファイル: Sort List.cs プロジェクト: zcyGit/My-Algor
        public ListNode MergeListNode(ListNode first, ListNode secend)
        {
            ListNode MergeListNode = new ListNode(-1);

            ListNode MergeListNodehead = MergeListNode;

            //相邻两组开始归并(每组都已排序)
            while (first != null && secend != null)
            {

                if (first.val > secend.val)
                {
                    MergeListNode.next = secend;
                    secend = secend.next;
                    MergeListNode = MergeListNode.next;
                }
                else
                {
                    MergeListNode.next = first;
                    first = first.next;
                    MergeListNode = MergeListNode.next;
                }
            }
            //把两组中剩余的值加入到列表中
            if (first != null)
            {
                MergeListNode.next = first;
            }

            if (secend != null)
            {
                MergeListNode.next = secend;

            }

            return MergeListNodehead.next;
        }
コード例 #24
0
        /// <summary>
        /// 思路:把中间后半段的链表反转,然后进行比较
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public static bool IsPalindrome(ListNode head)
        {
            if (head == null || head.next == null)
            {
                return true;
            }

            var begin = head;

            var mid = FindMid(head);
            mid = ReverseList(mid);

            while (mid != null)
            {
                if (begin.val != mid.val)
                {
                    return false;
                }
                begin = begin.next;
                mid = mid.next;

            }
            return true;
        }
コード例 #25
0
ファイル: Program.cs プロジェクト: zcyGit/My-Algor
        static void Add_Two_NumbersMain()
        {
            ListNode l1 = new ListNode(9);
            ListNode l2 = new ListNode(9);
            ListNode l3 = new ListNode(3);

            //l2.next = l3;
            l1.next = l2;

            ListNode m1 = new ListNode(9);
            ListNode m2 = new ListNode(6);
            ListNode m3 = new ListNode(4);

            //m2.next = m3;
            //m1.next = m2;

            int[] numbers = { -1, -2, -3, -4, -5 };

            var nodes = Add_Two_Numbers.AddTwoNumbers(l1, m1);

            Console.WriteLine(nodes.ToString());

        }
コード例 #26
0
 /// <summary>
 /// 明白题目的意思就很好做了
 /// </summary>
 /// <param name="node"></param>
 public static void DeleteNode(ListNode node)
 {
     node.val = node.next.val;
     node.next = node.next.next;
 }
コード例 #27
0
        public static void Test()
        {
            ListNode l1 = new ListNode(2);
            ListNode l2 = new ListNode(3);
            ListNode l3 = new ListNode(4);

            l2.next = l3;
            l1.next = l2;

            ListNode n1 = new ListNode(4);
            ListNode n2 = new ListNode(5);
            ListNode n3 = new ListNode(6);

            n2.next = n3;
            n1.next = n2;

            //ListNode[] lists = new ListNode[] { n1, l1 };

            ListNode[] lists = new ListNode[] { l1, null, n1 };

            var node = MergeKLists1(lists);
        }
コード例 #28
0
        public static ListNode MergeKLists1(ListNode[] lists)
        {
            if (lists == null || lists.Count() == 0)
                return null;
            if (lists.Count() == 1)
            {
                return lists[0];
            }
            ListNode node = new ListNode(-1);
            ListNode head = node;

            node.next = lists[0];
            for (int i = 1; i < lists.Count(); i++)
            {
                if (lists[i] != null)
                {
                    node.next = MergeListNode(node.next, lists[i]);
                }
            }

            return head.next;
        }
コード例 #29
0
ファイル: Add Two Numbers.cs プロジェクト: zcyGit/My-Algor
        public static ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            var l3 = new ListNode(0);

            if (l1 == null && l2 == null)
            {
                return null;
            }
            else if (l1 == null)
            {
                l3.val = l2.val;

                ListNode copyl2 = l2.next;
                l3.next = AddTwoNumbers(null, copyl2);
            }
            else if (l2 == null)
            {
                ListNode copyl1 = l1.next;

                if (l1.val >= 10)
                {
                    l3.val = l1.val - 10;
                    if (copyl1 != null)
                    {
                        copyl1.val += 1;
                    }
                    else
                    {
                        copyl1 = new ListNode(1);
                    }
                }
                else
                {
                    l3.val = l1.val;
                }

                l3.next = AddTwoNumbers(copyl1, null);
            }
            else
            {
                int sum = l2.val + l1.val;
                ListNode copyl1 = l1.next;

                if (sum >= 10)
                {
                    sum = sum - 10;
                    if (copyl1 != null)
                    {
                        copyl1.val += 1;
                    }
                    else
                    {
                        copyl1 = new ListNode(1);
                    }

                }

                l3.val = sum;
                l3.next = AddTwoNumbers(copyl1, l2.next);
            }

            return l3;
        }
コード例 #30
0
        /// <summary>
        /// 反转列表
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        protected static ListNode ReverseList(ListNode node)
        {
            ListNode head = new ListNode(0);
            head.next = node;

            ListNode current, pnext;
            current = head.next;

            while (current.next != null)
            {
                pnext = current.next;
                current.next = pnext.next;
                pnext.next = head.next;
                head.next = pnext;

            }

            return head.next;
        }