//这里特指删除当前节点
        private void DeleteNode(ListNode node)
        {
            if (node == null || node.next == null) return;

            node.val = node.next.val;
            node.next = node.next.next;
        }
Esempio n. 2
0
        private ListNode RotateRight(ListNode head, int k)
        {
            if (head == null || head.next == null || k == 0)
                return head;

            //将链表首尾相连
            int height = 1;
            ListNode pNode = head;
            while (pNode.next != null)
            {
                height++;
                pNode = pNode.next;
            }
            //如果后移的位数等于链表长度,则不需要移动
            if (k == height)
                return head;

            pNode.next = head;
            //找到新的首和尾
            int steps = height - k % height;

            pNode = head;
            while (--steps > 0)
            {
                pNode = pNode.next;
            }
            head = pNode.next;
            pNode.next = null;

            return head;
        }
        private ListNode AddTwoLinkedLists(ListNode l1, ListNode l2)
        {
            if (l1 == null || l2 == null)
                return (l1 == null ? ((l2 == null ? null : l2)) : l1);

            int temp = l1.val + l2.val;
            int dig = (temp >= 10) ? 1 : 0;
            ListNode nodeList = new ListNode(temp % 10);
            ListNode tempNodeList = nodeList;
            l1 = l1.next;
            l2 = l2.next;
            while (l1 != null || l2 != null)
            {
                temp = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + dig;
                dig = (temp >= 10) ? 1 : 0;
                tempNodeList.next = new ListNode(temp % 10);
                tempNodeList = tempNodeList.next;
                l1 = (l1 == null ? null : l1.next);
                l2 = (l2 == null ? null : l2.next);
            }
            if (dig == 1)
            {
                tempNodeList.next = new ListNode(dig);
                tempNodeList = tempNodeList.next;
            }
            return nodeList;
        }
Esempio n. 4
0
        //Iterative approach
        public ListNode ReverseList(ListNode head)
        {
            if ( head == null || head.next == null) {
                return head;
            }

            var newHead = head;
            var cur = newHead.next;
            var tmp = cur.next;

            newHead.next = null;

            while (cur != null) {
                cur.next = newHead;
                newHead = cur;
                cur = tmp;
                if (tmp == null) {
                    break;
                } else {
                    tmp = tmp.next;
                }
            }

            return newHead;
        }
Esempio n. 5
0
        public ListNode AddToHead(ListNode head, ListNode newItem)
        {
            head.next = null;
            newItem.next = head;

            return newItem;
        }
        public ListNode RemoveElements(ListNode head, int val)
        {
            if (head == null) {
                return null;
            }
            var tmp = head;

            while (tmp != null) {
                if (tmp.val == val) {
                    head = tmp.next;
                    tmp = tmp.next;
                } else {
                    break;
                }
            }

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

            tmp = head;
            while (tmp.next != null)
            {
                if (tmp.next.val == val) {
                    tmp.next = tmp.next.next;
                } else {
                    tmp = tmp.next;
                }
            }
            return head;
        }
        public bool UnitTest()
        {
            List<int> firstList = new List<int> { 2, 4, 6 };
            List<int> secondList = new List<int> { 5, 6 };

            #region firstList或者secondList为空,以及数量不等的判断
            if (firstList == null || firstList.Count == 0 || secondList == null || secondList.Count == 0)
            {
                return false;
            }
            #endregion

            #region 构造链表
            ListNode firstNodeList = new ListNode(firstList[0]);
            ListNode secondNodeList = new ListNode(secondList[0]);

            ListNode tempNodeList = firstNodeList;
            for (int i = 1; i < firstList.Count; i++)
            {
                tempNodeList.next = new ListNode(firstList[i]);
                tempNodeList = tempNodeList.next;
            }
            tempNodeList = secondNodeList;
            for (int i = 1; i < secondList.Count; i++)
            {
                tempNodeList.next = new ListNode(secondList[i]);
                tempNodeList = tempNodeList.next;
            }
            #endregion

            AddTwoLinkedLists(firstNodeList, secondNodeList);
            return true;
        }
Esempio n. 8
0
            public ListNode MergeTwoLists(ListNode l1, ListNode l2)
            {
                ListNode retHead = null, retP = null;

                ListNode p1 = l1, p2 = l2;
                ListNode pTmp = null;
                while (p1 != null || p2 != null) {

                    if (p1 == null) {
                        pTmp = p2;
                        p2 = null;
                    } else if (p2 == null) {
                        pTmp = p1;
                        p1 = null;
                    } else {
                        if (p1.val > p2.val) {
                            pTmp = p2;
                            p2 = p2.next;
                        } else {
                            pTmp = p1;
                            p1 = p1.next;
                        }
                    }

                    if (retP != null) {
                        retP.next = pTmp;
                    }
                    retP = pTmp;

                    if (retHead == null) {
                        retHead = retP;
                    }
                }
                return retHead;
            }
 public void UnitTest()
 {
     ListNode node = new ListNode(1);
     node.next = new ListNode(2);
     node.next.next = new ListNode(3);
     node.next.next.next = new ListNode(4);
     SwapPairs(node);
 }
 private bool HasCycle(ListNode head)
 {
     if (head == null) return false;
     ListNode walker = head, runner = head;
     while ((walker = walker.next) != null && runner.next != null && (runner = runner.next.next) != null)
         if (walker == runner) return true;
     return false;
 }
Esempio n. 11
0
            public ListNode ReverseKGroup(ListNode head, int k)
            {
                if (head == null) {
                    return head;
                }

                if (k == 1) {
                    return head;
                }

                ListNode root = null;
                ListNode pNode = head;
                ListNode[] pTmps = new ListNode[k];

                int i = 0;
                for (i = 0; i < k; ++i) {
                    if (pNode == null) {
                        return head;
                    }
                    pTmps [i] = pNode;
                    pNode = pNode.next;
                }

                ListNode aft = pTmps [i - 1].next;
                ListNode pre = null;

                while (true) {
                    if (root == null) {
                        root = pTmps [k - 1];
                    } else {
                        pre.next = pTmps [k - 1];
                    }

                    for (i = k-1; i > 0; --i) {
                        pTmps [i].next = pTmps [i - 1];
                    }
                    pTmps [0].next = aft;
                    pre = pTmps [0];

                    bool jumpout = false;
                    for (i = 0; i < k; ++i) {
                        if (pNode == null) {
                            jumpout = true;
                            break;
                        }
                        pTmps [i] = pNode;
                        pNode = pNode.next;
                    }
                    if (jumpout) {
                        break;
                    }
                    aft = pTmps [i - 1].next;
                }

                return root;
            }
Esempio n. 12
0
 public void UnitTest()
 {
     ListNode headNode = new ListNode(0);
     ListNode pNode = headNode;
     for (int index = 1; index < 6; index++)
     {
         ListNode elementNode = new ListNode(index);
         pNode.next = elementNode;
         pNode = pNode.next;
     }
     SortListDesc(headNode);
 }
        private ListNode RemoveElements(ListNode head, int val)
        {
            ListNode root = new ListNode(0),currentNode = root;
            root.next = head;

            while (currentNode.next != null)
            {
                if (currentNode.next.val == val) currentNode.next = currentNode.next.next;
                else currentNode = currentNode.next;
            }
            return root.next;
        }
Esempio n. 14
0
        public ListNode DetectCycle(ListNode head)
        {
            HashSet<ListNode> set = new HashSet<ListNode> ();
            var tmp = head;

            while (tmp != null) {
                if (set.Contains(tmp)) {
                    return tmp;
                }
                set.Add (tmp);
                tmp = tmp.next;
            }
            return null;
        }
Esempio n. 15
0
        public ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            ListNode head1 = l1;
            ListNode head2 = l2;

            ListNode newHead = null;
            ListNode newLast = null;

            while (head1 != null && head2 != null)
            {
                if (head1.val < head2.val) {
                    if (newHead == null) {
                        newHead = head1;
                        newLast = head1;
                    } else {
                        newLast.next = head1;
                        newLast = newLast.next;
                    }
                    head1 = head1.next;
                } else {
                    if (newHead == null) {
                        newHead = head2;
                        newLast = head2;
                    } else {
                        newLast.next = head2;
                        newLast = newLast.next;
                    }
                    head2 = head2.next;
                }
            }

            if (head1 != null && head2 == null) {
                if (newHead == null) {
                    newHead = head1;
                    newLast = head1;
                } else {
                    newLast.next = head1;
                }
            }else if(head1 == null && head2 != null){
                if (newHead == null) {
                    newHead = head2;
                    newLast = head2;
                } else {
                    newLast.next = head2;
                }
            }

            return newHead;
        }
Esempio n. 16
0
 public void UnitTest()
 {
     for (int k = 0; k < 10; k++)
     {
         ListNode headNode = new ListNode(0);
         ListNode pNode = headNode;
         for (int index = 1; index < 6; index++)
         {
             ListNode elementNode = new ListNode(index);
             pNode.next = elementNode;
             pNode = pNode.next;
         }
         RotateRight(headNode, k);
     }
 }
 private ListNode CreateListNode(List<int> lstNode)
 {
     ListNode rtnNodeList = null;
     if (lstNode.Count > 0)
     {
         rtnNodeList = new ListNode(lstNode[0]);
         ListNode tempNodeList = rtnNodeList;
         for (int i = 1; i < lstNode.Count; i++)
         {
             tempNodeList.next = new ListNode(lstNode[i]);
             tempNodeList = tempNodeList.next;
         }
     }
     return rtnNodeList;
 }
 private ListNode deleteDuplicates(ListNode head)
 {
     ListNode rtnNode = head;
     while (head != null && head.next != null)
     {
         if (head.val == head.next.val)
         {
             head.next = head.next.next;
         }
         else
         {
             head = head.next;
         }
     }
     return rtnNode;
 }
Esempio n. 19
0
        public ListNode MergeKLists(ListNode[] lists)
        {
            if (lists.Length == 0) { return null; }

            var result = new List<ListNode>(lists);
            ListNode head1, head2, newHead;
            while (result.Count > 1)
            {
                head1 = result[0];
                head2 = result[1];
                newHead = MergeTwoLists(head1, head2);
                result.RemoveRange(0, 2);
                result.Add(newHead);
            }

            return result[0];
        }
Esempio n. 20
0
            public ListNode SwapPairs(ListNode head)
            {
                if (head == null || head.next == null) {
                    return head;
                }

                ListNode root = null;
                ListNode pre = null, aft = null;
                ListNode p1 = head, p2 = head.next;
                if (p2 != null) {
                    aft = p2.next;
                }

                while (true) {
                    if (pre == null) {
                        root = p2;
                    } else {
                        pre.next = p2;
                    }

                    p2.next = p1;
                    p1.next = aft;
                    pre = p1;

                    if (pre != null) {
                        p1 = pre.next;
                    } else {
                        break;
                    }

                    if (p1 != null) {
                        p2 = p1.next;
                    } else {
                        break;
                    }

                    if (p2 != null) {
                        aft = p2.next;
                    } else {
                        break;
                    }
                }

                return root;
            }
Esempio n. 21
0
        public bool HasCycle(ListNode head)
        {
            var slow = head;
            var fast = head;

            while (slow != null) {
                slow = slow.next;

                if (fast == null || fast.next == null) {
                    return false;
                }
                fast = fast.next.next;
                if (slow == fast) {
                    return true;
                }
            }
            return false;
        }
        public void DeleteNode(ListNode node)
        {
            if (node == null) return;

            //if(node.next == null) return;

            while (node.next != null)
            {
                node.val = node.next.val;
                if (node.next.next != null)
                {
                    node = node.next;
                }
                else
                {
                    node.next = null;
                }
            }
        }
Esempio n. 23
0
            public ListNode createArray(int[] arr)
            {
                ListNode head = null, p = null;
                for (int i = 0; i < arr.Length; ++i) {
                    ListNode newNode = new ListNode (arr [i]);
                    if (head == null) {
                        head = newNode;
                    }
                    if (p == null) {
                        p = newNode;
                    } else {
                        p.next = newNode;
                    }

                    p = newNode;
                }

                return head;
            }
        public void UnitTest()
        {
            ListNode headNode = new ListNode(1);
            ListNode pNode = headNode;

            ListNode secondNode = new ListNode(2);
            pNode.next = secondNode;
            pNode = pNode.next;

            for (int index = 3; index < 1001; index++)
            {
                ListNode elementNode = new ListNode(index);
                pNode.next = elementNode;
                pNode = pNode.next;
            }
            //pNode.next = secondNode;
            //pNode = pNode.next;

            DetectCycle(headNode);
        }
        public void UnitTest()
        {
            ListNode headNode = new ListNode(0);
            ListNode pNode = headNode;

            ListNode secondNode = new ListNode(1);
            pNode.next = secondNode;
            pNode = pNode.next;

            for (int index = 1; index < 6; index++)
            {
                ListNode elementNode = new ListNode(index);
                pNode.next = elementNode;
                pNode = pNode.next;
            }
            pNode.next = secondNode;
            pNode = pNode.next;

            Console.WriteLine(HasCycle(headNode));
        }
        /*
         思路:本题可以使用快慢指针来解决,快指针一次走两步,慢指针一次走一步。有两个步骤:
         * 1.先找出是否有环及其快慢指针相遇的位置。
         * 2.然后再找出环的入口
         * 先来看第一个怎么解决:
         * 快慢指针初始都在头部,设链表长度为n。
         * 1.假如没有环,那么快指针最终会指向空指针。
         * 2.如果有环,那么慢指针最多走n步(即回到头部)就会和快指针相遇。
         * 如果快慢指针最终相遇了,就说明有环。那么如果找出环的入口位置呢
         * 假如从链表头到环入口长度为k(即D位置),快慢指针相遇在E,从相遇的节点到环入口长度为y。在相遇的时候快指针多走了一圈环形,那么快慢指针到相遇的时候各走的步数为:
         * slow = k + x;
         * fast = k + x + y + x;
         * 因为快指针速度比慢指针快一倍,因此有如下关系:fast = 2 * slow,即:(k + x + y +x) = 2 * (k + x),可以得出 k = y。
         * 因此现在从快慢指针相遇的地方到环入口和从链表头到环入口的距离相等,所以让快慢指针以相同的速度从这两个地方再开始走,到相遇的地方即环的入口。
         */
        private ListNode DetectCycle(ListNode head)
        {
            if (head == null || head.next == null || head.next.next == null) return null;
            ListNode walker = head, runner = head;
            while (walker.next != null && runner.next != null && runner.next.next != null)
            {
                walker = walker.next;
                runner = runner.next.next;
                if (walker == runner) break;
            }

            if (walker != runner) return null;
            walker = head;
            while (walker != runner)
            {
                walker = walker.next;
                runner = runner.next;
            }
            return walker;
        }
        public ListNode OddEvenList(ListNode head)
        {
            if (head == null)
            {
                return(head);
            }

            ListNode odd     = head;
            ListNode oddEnd  = odd;
            ListNode even    = head.next;
            ListNode evenEnd = even;

            ListNode node = even?.next;

            int index = 3;

            while (node != null)
            {
                if (index % 2 == 0)
                {
                    evenEnd.next = node;
                    evenEnd      = node;
                }
                else
                {
                    oddEnd.next = node;
                    oddEnd      = node;
                }

                node = node.next;
                index++;
            }

            oddEnd.next = even;

            if (evenEnd != null)
            {
                evenEnd.next = null;
            }
            return(odd);
        }
Esempio n. 28
0
        public bool IsPalindrome(ListNode head)
        {
            var stack = new Stack <int>();
            var h     = head;

            while (h != null)
            {
                stack.Push(h.val);
                h = h.next;
            }
            while (head != null)
            {
                var val = stack.Pop();
                if (head.val != val)
                {
                    return(false);
                }
                head = head.next;
            }
            return(true);
        }
        private ListNode SwapPairs(ListNode head)
        {
            var root = new ListNode(0);
            root.next = head;
            var current = root.next;
            var prev = root;
            

            ListNode node = null;
            while (current != null && current.next != null)
            {
                node = current.next;
                current.next = node.next;
                node.next = current;
                prev.next = node;

                prev = prev.next.next;
                current = prev.next;
            }
            return root.next;
        }
Esempio n. 30
0
        /** Delete the index-th node in the linked list, if the index is valid. */
        public void DeleteAtIndex(int index)
        {
            if (index < 0 || index > Length - 1)
            {
                return;
            }
            if (index == 0)
            {
                head = head.next;
                Length--;
                return;
            }
            var runNode = head;

            for (int i = 0; i < index - 1; i++)
            {
                runNode = runNode.next;
            }
            runNode.next = runNode.next.next;
            Length--;
        }
Esempio n. 31
0
        /// <summary>
        /// 思路:遍历,然后List存储每个节点,判断List是否存在该节点,存在则是环形链表。
        /// 另一种思路:快慢指针,同时起步,快指针总会跑过慢指针,快指针等于慢指针时就是环形链表,若快指针跑完了就不是
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public static bool HasCycle(ListNode head)
        {
            bool       isCycle = false;
            List <int> keyList = new List <int>();
            ListNode   current = head;

            while (current != null)
            {
                if (keyList.Contains(current.GetHashCode()))
                {
                    isCycle = true;
                    break;
                }
                else
                {
                    keyList.Add(current.GetHashCode());
                    current = current.next;
                }
            }
            return(isCycle);
        }
Esempio n. 32
0
        public ListNode GetTestData()
        {
            var head = new ListNode(1);

            var node1 = new ListNode(3);

            head.next = node1;

            var node2 = new ListNode(2);

            node1.next = node2;

            var node3 = new ListNode(3);

            node2.next = node3;

            var node4 = new ListNode(1);

            node3.next = node4;

            return(head);
        }
Esempio n. 33
0
        /// <summary>
        /// 思路:遍历两个链表的每个元素拿出来比较,小的先插入到新链表里,小的链表下移。另一种思路:递归
        /// </summary>
        /// <param name="l1"></param>
        /// <param name="l2"></param>
        /// <returns></returns>
        public static ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            ListNode result  = new ListNode(0);
            ListNode current = result;

            while (l1 != null && l2 != null)
            {
                if (l1.val < l2.val)
                {
                    current.next = l1;
                    l1           = l1.next;
                }
                else
                {
                    current.next = l2;
                    l2           = l2.next;
                }
                current = current.next;
            }
            current.next = l1 == null ? l2 : l1;
            return(result.next);
        }
Esempio n. 34
0
        public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
        {
            var b = headB;

            while (headA != null)
            {
                if (headA.val == 8)
                {
                }
                while (headB != null)
                {
                    if (headB.Equals(headA))
                    {
                        return(headA);
                    }
                    headB = headB.next;
                }
                headB = b;
                headA = headA.next;
            }
            return(null);
        }
Esempio n. 35
0
        /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
        public void AddAtIndex(int index, int val)
        {
            if (index == 0)
            {
                AddAtHead(val);
                return;
            }

            var temp = head;

            while (temp != null && index != 1)
            {
                temp = temp.next;
                index--;
            }

            if (temp != null && index == 1)
            {
                var newNode = new ListNode(val);
                newNode.next = temp.next;
                temp.next    = newNode;
            }
        }
Esempio n. 36
0
            public int GetDecimalValue(ListNode head)
            {
                int sum   = 0;
                int index = 0;

                int[] digits = new int[30];
                while (head != null)
                {
                    for (int i = 0; i < index; i++)
                    {
                        digits[i] *= 2;
                    }
                    digits[index] = head.val;
                    index++;
                    head = head.next;
                }

                for (int i = 0; i < digits.Length; i++)
                {
                    sum += digits[i];
                }
                return(sum);
            }
Esempio n. 37
0
        ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            var head = new ListNode(-1);
            var current = head;

            while (l1 != null && l2 != null)
            {
                if (l1.val <= l2.val)
                {
                    current.next = l1;
                    l1 = l1.next;
                }
                else
                {
                    current.next = l2;
                    l2 = l2.next;
                }
                current = current.next;
            }

            current.next = l1 == null ? l2 : l1;
            return head.next;
        }
Esempio n. 38
0
        public ListNode RotateRight(ListNode head, int k)
        {
            if (null == head || null == head.next)
            {
                return(head);
            }

            int useLegth = 0;

            var tempIndex = head;


            while (null != tempIndex)
            {
                tempIndex = tempIndex.next;
                useLegth++;
            }

            ListNode curr = head;

            for (int i = 0; i < k % useLegth; i++)
            {
                ListNode x    = curr;
                ListNode prev = new ListNode(-1);
                prev.next = curr;

                while (curr.next != null)
                {
                    curr = curr.next;
                    prev = prev.next;
                }
                prev.next = null;
                curr.next = x;
            }

            return(curr);
        }
        public ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            if (l1 == null && l2 == null)
            {
                return(null);
            }

            //如果其中一个节点为空了,则新节点的下个节点就是当前节点
            if (l1 == null)
            {
                return(l2);
            }
            if (l2 == null)
            {
                return(l1);
            }
            ListNode node = null;

            //进行比较
            if (l1.val <= l2.val)
            {
                node = new ListNode(l1.val);
                l1   = l1.next;
            }
            else
            {
                node = new ListNode(l2.val);
                l2   = l2.next;
            }

            var value = MergeTwoLists(l1, l2);

            node.next = value;


            return(node);
        }
Esempio n. 40
0
        public ListNode RemoveNthFromEnd(ListNode head, int n)
        {
            var firstPointer = head;
            var secondPointer = head;

            int i = 0;
            while (i < n) {
                firstPointer = firstPointer.next;
                i += 1;
            }

            if (firstPointer == null) {
                return head.next;
            }

            while (firstPointer.next != null) {
                firstPointer = firstPointer.next;
                secondPointer = secondPointer.next;
            }

            secondPointer.next = secondPointer.next.next;

            return head;
        }
Esempio n. 41
0
        public ListNode CreateSampleList(Queue <int> queue, ListNode listNode)
        {
            var templist = listNode;

            while (queue.Count > 0)
            {
                if (templist.val == 0 && templist.next == null)
                {
                    templist.val = queue.Dequeue();
                    if (queue.Count != 0)
                    {
                        templist.next = new ListNode();
                    }
                    continue;
                }
                templist     = templist.next;
                templist.val = queue.Dequeue();
                if (queue.Count != 0)
                {
                    templist.next = new ListNode();
                }
            }
            return(listNode);
        }
Esempio n. 42
0
 public ListNode SortList(ListNode head)
 {
     return(MergeSort(head));
 }
Esempio n. 43
0
        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            int      p = 0, c = 0;
            ListNode root = new ListNode(0);
            ListNode node = root;

            while (l1 != null || l2 != null)
            {
                if (l1 != null && l2 != null)
                {
                    if (l1.val + l2.val + c >= 10)
                    {
                        node.next = new ListNode(l1.val + l2.val + c - 10);
                        c         = 1;
                    }
                    else
                    {
                        node.next = new ListNode(l1.val + l2.val + c);
                        c         = 0;
                    }
                    l1 = l1.next;
                    l2 = l2.next;
                }
                else if (l1 != null)
                {
                    if (l1.val + c >= 10)
                    {
                        node.next = new ListNode(l1.val + c - 10);
                        c         = 1;
                    }

                    else
                    {
                        node.next = new ListNode(l1.val + c);
                        c         = 0;
                    }
                    l1 = l1.next;
                }
                else if (l2 != null)
                {
                    if (l2.val + c >= 10)
                    {
                        node.next = new ListNode(l2.val + c - 10);
                        c         = 1;
                    }

                    else
                    {
                        node.next = new ListNode(l2.val + c);
                        c         = 0;
                    }

                    l2 = l2.next;
                }

                node = node.next;
            }
            if (c == 1)
            {
                node.next = new ListNode(1);
            }
            return(root.next);
        }
Esempio n. 44
0
        public static void Select(int i)
        {
            string result = "";

            switch (i)
            {
            case 0:    //两数之和
                //给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
                //你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
                int[] nums   = { 2, 7, 11, 15 };
                int   target = 9;
                result = TwoSum(nums, target).ToString();
                break;

            case 1:    //寻找两个正序数组的中位数------不会,抄的
                //给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。
                //请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m +n))。
                //你可以假设 nums1 和 nums2 不会同时为空。
                int[] nums1_1 = { 1, 4, 7, 9 };
                int[] nums1_2 = { 1, 2, 3, 4, 5, 6, 7, 8 };
                result = FindMedianSortedArrays(nums1_1, nums1_2).ToString();
                break;

            case 2:    //最长回文子串
                //给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。例如"cabccbad"的最长回文子串是"abccba"
                string s = "cccc";
                result = LongestPalindrome(s);
                break;

            case 3:    //最长公共前缀
                //编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ""。例如输入:["flower","flow","flight"],输出: "fl"
                string[] strs = { "", "b" };
                result = LongestCommonPrefix(strs);
                break;

            case 4:    //三数之和
                int[] nums4 = { 0, 0, 0 };
                var   r     = ThreeSum(nums4);
                result = JsonConvert.SerializeObject(r);
                break;

            case 5:    //合并两个有序数组
                //注意是两个有序的数组。
                int[] nums5_1 = { 1, 2, 3, 0, 0, 0 }; int m5 = 3;
                int[] nums5_2 = { 2, 5, 6 }; int n5 = 3;
                Merge(nums5_1, m5, nums5_2, n5);
                result = JsonConvert.SerializeObject(nums5_1);
                break;

            case 6:    //螺旋矩阵
                //给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。
                int[][] nums6 = new int[][] { new int[] { 1, 2, 3, 4 }, new int[] { 5, 6, 7, 8 }, new int[] { 9, 10, 11, 12 } };
                result = JsonConvert.SerializeObject(SpiralOrder(nums6));
                break;

            case 7:    //螺旋矩阵II
                //给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。
                result = JsonConvert.SerializeObject(GenerateMatrix(3));
                break;

            case 8:    //存在重复元素
                //给定一个整数数组,判断是否存在重复元素。如果任意一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
                int[] nums8 = { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 };
                result = ContainsDuplicate(nums8).ToString();
                break;

            case 9:    //反转字符串
                //编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。
                //不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
                char[] nums9 = { 'H', 'a', 'n', 'n', 'a', 'h' };
                ReverseString(nums9);
                result = JsonConvert.SerializeObject(nums9);
                break;

            case 10:    //盛最多水的容器
                //给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为(i, ai) 和(i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
                int[] nums10 = { 1, 8, 6, 2, 5, 4, 8, 3, 7 };
                result = MaxArea(nums10).ToString();
                break;

            case 11:    //删除排序数组中的重复项
                //给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
                //不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。你不需要考虑数组中超出新长度后面的元素。
                int[] nums11 = { 0, 1, 2, 3, 4 };
                result = RemoveDuplicates(nums11).ToString();
                break;

            case 12:    //有效的括号
                //给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
                string str12 = "{[()]}()";
                result = IsValid(str12).ToString();
                break;

            case 13:    //反转一个单链表。
                //输入: 1->2->3->4->5->NULL
                //输出: 5->4->3->2->1->NULL
                ReverseList(null);
                break;

            case 14:    //两数相加
                //给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
                //如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
                //您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
                ListNode l14_1 = new ListNode(9);

                int[]    num14_2 = { 1, 9, 9, 9, 9, 9, 9, 9, 9, 9 };
                ListNode l14_2   = new ListNode(0);
                ListNode current = l14_2;
                foreach (int i14 in num14_2)
                {
                    current.next = new ListNode(i14);
                    current      = current.next;
                }
                var Node14 = AddTwoNumbers(l14_1, l14_2.next);
                break;

            case 15:    //合并两个有序链表
                ListNode l15_1 = new ListNode(-9);
                l15_1.next = new ListNode(3);

                ListNode l15_2 = new ListNode(5);
                l15_2.next = new ListNode(7);

                var Node15 = MergeTwoLists(l15_1, l15_2);
                break;

            case 16:    //旋转链表
                //给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。
                //输入: 1->2->3->4->5->NULL, k = 2
                //输出: 4->5->1->2->3->NULL
                ListNode l16_1 = new ListNode(9);
                l16_1.next = new ListNode(3);
                var Node16 = RotateRight(l16_1, 4);
                break;

            case 17:    //环形链表
                HasCycle(null);
                break;

            case 18:    //相交链表
                //编写一个程序,找到两个单链表相交的起始节点。
                GetIntersectionNode(null, null);
                break;

            case 19:    //删除链表中的节点
                //请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。注意:传入函数的唯一参数为 要被删除的节点 。
                DeleteNode(null);
                break;

            case 20:    //整数反转
                //给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
                Reverse(-123);
                break;

            case 21:    //回文数
                //判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
                //你能不将整数转为字符串来解决这个问题吗?
                IsPalindrome(1073773701);
                break;

            case 22:    //只出现一次的数字
                //给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
                //你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
                SingleNumber(new int[] { 5, 1, 4, 4, 1 });
                break;
            }
            Console.WriteLine(result);
        }
Esempio n. 45
0
 /// <summary>
 /// 跳过该节点,指向下一个节点
 /// </summary>
 /// <param name="node">该节点就是要被删除的节点</param>
 public static void DeleteNode(ListNode node)
 {
     node.val  = node.next.val;
     node.next = node.next.next;
 }
Esempio n. 46
0
 /** Initialize your data structure here. */
 public MyLinkedList()
 {
     head   = null;
     Length = 0;
 }
Esempio n. 47
0
        public ListNode MergeTwoLists(ListNode l1, ListNode l2)
        {
            if (l1 == null)
            {
                return(l2);
            }
            if (l2 == null)
            {
                return(l1);
            }

            var      p1 = l1;
            var      p2 = l2;
            ListNode head;
            ListNode current = null;

            if (l1.val > l2.val)
            {
                head = l2;
            }
            else
            {
                head = l1;
            }

            while (p1 != null && p2 != null)
            {
                if (p1.val > p2.val)
                {
                    if (current != null)
                    {
                        current.next = p2;
                        current      = current.next;
                    }
                    else
                    {
                        current = p2;
                    }
                    p2 = p2.next;
                }
                else
                {
                    if (current != null)
                    {
                        current.next = p1;
                        current      = current.next;
                    }
                    else
                    {
                        current = p1;
                    }
                    p1 = p1.next;
                }
            }

            if (p1 == null)
            {
                current.next = p2;
            }
            else
            {
                current.next = p1;
            }
            return(head);
        }
Esempio n. 48
0
 public ListNode(int x)
 {
     val  = x;
     next = null;
 }
Esempio n. 49
0
File: 02.05.cs Progetto: ZY-233/-PY-
 public ListNode(int m)
 {
     val = m;
     next = null;
 }
Esempio n. 50
0
 public ListNode(int val = 0, ListNode next = null)
 {
     this.val  = val;
     this.next = next;
 }
Esempio n. 51
0
            public static ListNode createListByArray(int [] nums)
            {
                ListNode root = null;
                ListNode pTmp = null;

                for (int i = 0; i < nums.Length; ++i) {
                    ListNode p = new ListNode (nums [i]);

                    if (pTmp == null) {
                        pTmp = p;
                    } else {
                        pTmp.next = p;
                        pTmp = p;
                    }

                    if (root == null) {
                        root = p;
                    }
                }

                return root;
            }
Esempio n. 52
0
 /** Initialize your data structure here. */
 public MyLinkedList()
 {
     head = null;
 }
Esempio n. 53
0
        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)
        {
            ListNode result  = null;
            ListNode current = null;
            int      inc     = 0;
            var      sum     = 0;

            while (l1 != null && l2 != null)
            {
                sum = l1.val + l2.val + inc;
                if (sum >= 10)
                {
                    sum -= 10;
                    inc  = 1;
                }
                else
                {
                    inc = 0;
                }

                if (result == null)
                {
                    result = current = new ListNode(sum);
                }
                else
                {
                    current.next = new ListNode(sum);
                    current      = current.next;
                }

                l1 = l1.next;
                l2 = l2.next;
            }

            while (l1 != null)
            {
                sum = l1.val + inc;
                if (sum >= 10)
                {
                    sum -= 10;
                    inc  = 1;
                }
                else
                {
                    inc = 0;
                }

                current.next = new ListNode(sum);
                current      = current.next;
                l1           = l1.next;
            }

            while (l2 != null)
            {
                sum = l2.val + inc;
                if (sum >= 10)
                {
                    sum -= 10;
                    inc  = 1;
                }
                else
                {
                    inc = 0;
                }

                current.next = new ListNode(sum);
                current      = current.next;
                l2           = l2.next;
            }

            if (inc == 1)
            {
                current.next = new ListNode(inc);
            }

            return(result);
        }